Multithreading is a process in which multiple threads execute simultaneously within a program.
A thread is the smallest unit of
execution in a process. Multithreading allows a program to perform many tasks at the same time. For example, while typing in a
text editor, spell checking can run in the background at the same time. This is possible because of multithreading.
Java provides built-in support for multithreading, which helps in developing fast and efficient applications.
Advantages of Multithreading Improves performance of programs. Saves CPU time. Makes applications more responsive.
Allows multiple tasks to run simultaneously. Better utilization of system resources.
Life Cycle of a Thread A thread passes through different states during execution. 1. New State The thread is created but not
started. 2. Runnable State The thread is ready to run and waiting for CPU time. 3. Running State The thread is executing its task.
4. Blocked/Waiting State The thread waits for another thread or resource. 5. Dead State The thread finishes execution.
Creation of Thread in Java Threads can be created in two ways: 1. By Extending Thread Class In this method, a class extends
the Thread class and overrides the run() method. The thread starts execution using the start() method.
Steps Create a class that extends Thread. Override the run() method. Create object of the class. Call start() method.
2. By Implementing Runnable Interface In this method, a class implements the Runnable interface and defines the run() method.
Steps Implement Runnable interface. Override the run() method. Create object of the class. Pass object to Thread class
constructor. Call start() method.
Thread Model in Java The Thread Model describes how threads behave and interact during execution. Java follows a
multithreaded programming model where many threads run independently.
Main Features of Thread Model 1. Thread Priority Each thread has a priority that helps the scheduler decide which thread
executes first. Minimum Priority = 1 Normal Priority = 5 Maximum Priority = 10 Higher priority threads usually get CPU
time first. 2. Thread Scheduling Thread scheduling is the process of selecting which thread will run next.
The Java scheduler manages execution of threads. Two common scheduling methods are: a) Preemptive Scheduling
Higher priority thread interrupts lower priority thread. b) Time Slicing Each thread gets CPU time for a fixed period.
3. Synchronization When multiple threads access shared resources, problems may occur. Synchronization controls access to
shared data so that only one thread works at a time. It prevents data inconsistency. 4. Inter-Thread Communication Threads
can communicate with each other using methods like: wait() notify() notifyAll() This improves coordination between threads.
5. Daemon Thread Daemon threads run in the background and support other threads. Like: Garbage Collector Background services
Important Methods of Thread Class start() → starts thread execution run() → contains code executed by thread
sleep() → pauses thread for some time join() → waits for another thread to finish stop() → stops thread (deprecated)
Applications of Multithreading Gaming applications Animation programs Banking systems Online chatting applications
Web servers
File Handling in Java is the process of creating, reading, writing, updating, and deleting files. It allows data to be stored permanently
in a computer instead of temporary memory. Java provides file handling support through the [Link] package.
File handling is important in applications such as banking systems, text editors, school management systems, and databases where
permanent storage of data is needed. Need of File Handling To store data permanently To read and write data from files
To manage large amounts of data To exchange data between programs To maintain records and reports
File Class in Java Java provides the File class to perform file and directory operations. It is available in the [Link] package.
The File class is used for: Creating files Deleting files Checking file existence Getting file information
Creating folders and directories Types of File Operations 1. Creating a File A new file can be created for storing data.
2. Writing into a File Data can be written into a file using output stream classes. 3. Reading from a File Stored data can be
read from a file using input stream classes. 4. Appending Data New data can be added to an existing file without deleting old data.
5. Deleting a File Unused or unwanted files can be removed from the system
Classes Used in File Handling File – Used for file and directory operations. FileReader – Used to read character data from files.
FileWriter – Used to write character data into files. BufferedReader – Used for efficient reading of text.
BufferedWriter – Used for efficient writing of text. PrintWriter – Used to print formatted data into files.
Advantages of File Handling Provides permanent storage of data Makes data management easy
Helps in sharing data between applications Useful for maintaining records Supports large data storage
Disadvantages of File Handling File operations are slower than memory operations Improper handling may cause data loss
Requires exception handling for safe execution Large files may consume more storage space
String and StringBuffer are used to store text in Java, but they are different in many ways. A StringBuffer object is mutable, which
means its value can be changed after creation. A String object is immutable, which means its value cannot be changed after creation.
String is mainly used when data does not change frequently, while StringBuffer is used when text needs to be modified many times.
String in Java A String is a sequence of characters enclosed within double quotes. Once a String object is created, its value cannot
be changed. If any modification is made, a new object is created in memory.
Features of String String objects are immutable. Stored in String Constant Pool memory. More secure because values cannot
be changed. Suitable for fixed text data. Slower when many modifications are required because new objects are created repeatedly
StringBuffer in Java StringBuffer is a class used to create mutable strings. The content of a StringBuffer object can be changed
without creating a new object. It provides methods like append(), insert(), delete(), and reverse() for modifying strings.
Features of StringBuffer StringBuffer objects are mutable. Changes are made in the same object.
Faster for repeated modifications. Consumes less memory during modifications. Suitable for dynamic text data.
Differences Between String and StringBuffer String uses more memory during modification, while StringBuffer uses less memory.
In String, modification creates a new object, while in StringBuffer modification happens in the same object.
String is immutable, whereas StringBuffer is mutable. String is slower for repeated changes, whereas StringBuffer is faster.
String objects can be created using string literals, while StringBuffer objects are created using constructors.
String is more secure because its value cannot be changed, while StringBuffer is less secure because values can change.
String supports concatenation using + operator, while StringBuffer uses methods like append().
String is used for fixed data, whereas StringBuffer is used for changing data. Advantages of StringBuffer Faster performance
for modifications. Saves memory during changes. Provides many useful methods for string operations.
Advantages of String Easy to use. Provides security because data cannot change. Suitable for constant data.
JVM (Java Virtual Machine) is a virtual machine that runs Java programs. It converts Java bytecode into machine language and
allows Java programs to run on different platforms. JVM also manages memory and provides security during execution.
The final keyword is used to restrict modification in Java. A final variable cannot be changed, a final method cannot be overridden,
and a final class cannot be inherited. It helps provide security and fixed behavior in programs.
The this keyword refers to the current object of a class. It is used to access current class variables, methods, and constructors. It
helps remove confusion between instance variables and local variables.
An Exception is an unwanted event or error that occurs during program execution. It interrupts the normal flow of the program. Java
handles exceptions using try, catch, throw, and finally blocks.
CLASSPATH is an environment variable in Java used to locate class files and packages. It tells the JVM and compiler where Java
classes are stored. Without proper CLASSPATH, Java programs may not run correctly.
An interface is used in Java to achieve complete abstraction. It contains abstract methods that are implemented by other classes.
Interfaces also support multiple inheritance and improve flexibility in programs.
StringBuffer is a class in Java used to create mutable strings. Unlike String, its content can be changed without creating a new
object. It provides methods like append(), insert(), delete(), and reverse() for string modification.
Programming paradigms are different styles or approaches of programming. Common paradigms are Procedural Programming,
Object-Oriented Programming, Functional Programming, and Logical Programming. They help programmers solve problems in
different ways.
Interface and Abstract Class are important concepts in Java used to achieve abstraction. They help in hiding unnecessary details and
showing only important features of a program. Although both are used for similar purposes, they are different in many ways.
An Abstract Class is a class that can contain both abstract methods and normal methods. It is used when different classes share
common features and behavior. An Interface is used to define a set of rules that classes must follow. It mainly contains
abstract methods and is used to achieve complete abstraction. Interface An interface is declared using the interface keyword.
It contains methods that are implemented by other classes. A class uses the implements keyword to implement an interface.
Interfaces are mainly used when different classes need to follow the same behavior even if they are unrelated.
Features of Interface Interface provides complete abstraction. All methods are public and abstract by default.
Variables inside interface are public, static, and final by default. Interface does not contain constructors. A class can implement
more than one interface. Interfaces support multiple inheritance in Java. It is mainly used for security and flexibility.
Abstract Class An abstract class is declared using the abstract keyword. It may contain abstract methods as well as normal
methods with body. An abstract class is inherited using the extends keyword. Abstract classes are used when classes have
common properties and methods. Features of Abstract Class Abstract class can contain abstract and non-abstract methods.
It can contain constructors and variables. It provides partial abstraction. Objects of abstract class cannot be created directly.
A class can extend only one abstract class. Methods can have different access modifiers like private, protected, and public.
It is used when related classes share common behavior.
Differences Between Interface and Abstract Class Abstract class can have constructors, but interface cannot have constructors.
An abstract class is declared using the abstract keyword, while an interface is declared using the interface keyword.
Abstract class can contain both abstract and normal methods, whereas interface mainly contains abstract methods.
Abstract class supports partial abstraction, while interface supports complete abstraction.
Variables in abstract class can be normal variables, but variables in interface are public static final by default.
A class can extend only one abstract class, but it can implement multiple interfaces.
Abstract class uses the extends keyword, while interface uses the implements keyword.
Abstract classes are used when classes are closely related, while interfaces are used when unrelated classes need common behavior.
Abstract class is faster in some cases because it contains normal methods, while interface may be slightly slower due to extra
abstraction. Methods in abstract class can have any access modifier, but methods in interface are public by default.
Advantages of Interface Supports multiple inheritance. Provides high security. Makes program flexible.
Helps in loose coupling between classes. Achieves complete abstraction. Advantages of Abstract Class Reduces code duplication
Provides common functionality to subclasses Easy to maintain shared code. Supports partial abstraction. Improves code reusability.
Polymorphism is one of the main features of Object-Oriented Programming (OOP) in Java. The word polymorphism means
“many forms.” It means one object or method can perform different actions in different situations. Polymorphism allows the same
method name to be used for different purposes. It increases flexibility and makes programming easier and more efficient.
In Java, polymorphism mainly helps programmers write reusable and manageable code. It also improves the relationship between
classes and objects. Types of Polymorphism in Java 1. Compile-Time Polymorphism This type of polymorphism is
achieved by method overloading. In method overloading, multiple methods have the same name but different parameters.
The compiler decides which method to call during compile time, so it is called compile-time polymorphism.
2. Runtime Polymorphism This type of polymorphism is achieved by method overriding. In method overriding, a child class
provides its own version of a method already defined in the parent class. The method call is decided during runtime, so it is
called runtime polymorphism. Features of Polymorphism One method can behave differently. Reduces code duplication.
Supports method overloading and overriding. Helps in achieving flexibility in programs. Makes code reusable and easy to manage.
Advantages of Polymorphism Improves code reusability. Makes programs flexible and extensible.
Simplifies program maintenance. Reduces complexity in coding. Helps achieve dynamic behavior in Java.
Disadvantages of Polymorphism Program logic may become difficult to understand. Runtime polymorphism can slightly reduce
performance. Debugging becomes harder in large programs.
Inheritance is one of the most important features of Object-Oriented Programming (OOP) in Java. It allows one class to acquire the
properties and behaviors of another class. The class that inherits is called the subclass (child class) and the class whose properties are
inherited is called the superclass (parent class). Inheritance helps in code reusability, reduces duplication, and makes programs
easier to manage and extend. By using inheritance, programmers can create new classes based on existing classes.
In Java, inheritance is achieved by using the extends keyword. Types of Inheritance in Java 1. Single Inheritance When one
child class inherits one parent class. example class A { void show() { [Link]("Class A"); } } class B
extends A { void display() { [Link]("Class B"); } }
2. Multilevel Inheritance When a class inherits another class, and another class further inherits it.
class A { void show() { [Link]("Class A"); }} class B extends A { } class C extends B { }
Here, class C inherits properties of both A and B.
3. Hierarchical Inheritance When multiple child classes inherit the same parent class.
class A { void display() { [Link]("Parent Class"); } } class B extends A { } class C extends A { }
Both B and C inherit from class A. 4. Multiple InheritanceJava does not support multiple inheritance through classes because it may
create ambiguity problems. However, it can be achieved using interfaces. Means class can inherit property from more than 1 class.
Advantages of Inheritance Code Reusability – Existing code can be reused in new classes. Saves Time and Effort – No need to
write the same code again. Improves Maintainability – Easy to update and manage code. Supports Method Overriding –
Helps achieve runtime polymorphism. Represents Real-World Relationships – Such as Animal → Dog, Vehicle → Car.
Disadvantages of Inheritance Increases coupling between classes. Improper use can make programs complex.
Changes in parent class may affect child classes.
Evolution of OO Methodology Object-Oriented methodology evolved to overcome the limitations of procedural programming.
Earlier programming focused only on functions, but OOP introduced classes and objects for better security, reusability, and
maintenance. Languages like C++, Java, and Python popularized OO methodology.
Overloading Constructor Constructor overloading means creating multiple constructors in the same class with different
parameters. It allows objects to be initialized in different ways. Java decides which constructor to call based on the arguments passed.
Package Naming A package in Java is a collection of related classes and interfaces. Package naming helps organize programs and
avoid naming conflicts. Java package names are usually written in lowercase, such as [Link] or [Link].
Synchronization in Java Synchronization is a process used to control access to shared resources in multithreading. It prevents
multiple threads from accessing the same data at the same time. Synchronization helps avoid data inconsistency and thread
interference.
Extends Keyword in Java is used to achieve inheritance. It allows one class to inherit the properties and methods of another class.
The child class can reuse the code of the parent class, which improves code reusability.
Runtime Exceptions are exceptions that occur during the execution of a program. They are also called unchecked exceptions
because the compiler does not check them at compile time. Examples include ArithmeticException, NullPointerException, and
ArrayIndexOutOfBoundsException.