Module 2 Java
Module 2 Java
● super reference
● Constructor chaining
● Method Overriding
● final keyword
○ Abstract Classes
○ Abstract Methods
○ Defining Interface
○ Implementing Interface
○ Extending Interface
○ Interface References
● Exception Handling
○ Types of Exception
○ Exception Classes
○ Uncaught Exceptions
○ Handling Exception
○ User-defined Exception
● Multithreaded Programming
○ Runnable Interface
○ Thread Class
○ Thread Creation
○ Thread Scheduling
○ Inter-thread Communication
○ Joining Threads
📙 Module II: Inheritance, Abstract Classes and Interfaces, Exception Handling, and
Multithreaded Programming
📘 Inheritance in Java
Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows one
class (called a subclass or derived class) to inherit fields and methods from another class
(called a superclass or base class). In simpler words, inheritance lets us create a new class
from an existing class so that we can reuse its code without rewriting it. This makes the program
more modular and easier to manage.
Key Benefits:
● Multilevel Inheritance – A class inherits from a class, which itself inherits from another
class.
In Java, a reference variable of a superclass can refer to an object of its subclass. This is called
upcasting and allows runtime polymorphism. It enables method calls to behave differently
based on the actual object type, even if the reference type is of the superclass.
Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Animal a = new Dog();
a.sound(); // Output: Dog barks
The super keyword in Java is used to refer to the immediate parent class object. It is used in
the following ways:
Example:
class Animal {
String color = "white";
}
class Dog extends Animal {
String color = "black";
void printColor() {
System.out.println(super.color); // prints white
}
}
Constructor Chaining
Constructor chaining is the process of calling one constructor from another in the same class or
from the superclass. This ensures all the constructors are executed in the correct order during
object creation.
Example:
class A {
A() {
System.out.println("Constructor of A");
}
}
class B extends A {
B() {
super(); // calls constructor of A
System.out.println("Constructor of B");
}
}
Method Overriding
Method overriding means redefining a method of the superclass in a subclass. This is done to
provide specific behavior in the subclass.
Rules of Overriding:
Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
final Keyword
The final keyword is used for constant declaration and to restrict inheritance or method
overriding.
Example:
An abstract class is a class that cannot be instantiated. It is used to define common behavior
and can have both abstract (no body) and non-abstract (with body) methods. It provides partial
abstraction.
Example:
Abstract Methods
An abstract method has no body. Subclasses must override it to provide specific
implementation.
Defining Interface
An interface in Java is a collection of abstract methods. From Java 8, interfaces can also
contain default and static methods. Interfaces support full abstraction and multiple
inheritance.
Example:
interface Drawable {
void draw();
default void msg() {
System.out.println("Default message");
}
}
Implementing Interface
A class uses the implements keyword to use an interface. It must override all abstract
methods of the interface.
Example:
Extending Interface
One interface can inherit another using the extends keyword. This helps in organizing related
interfaces.
Example:
interface A {
void show();
}
interface B extends A {
void display();
}
Interface References
A reference of an interface type can hold the object of a class that implements the interface.
This helps in achieving polymorphism.
Example:
❗ Exception Handling
Hierarchy of Exception Classes
● Throwable
○ Exception
Examples:
● Checked: FileNotFoundException
● Unchecked: ArithmeticException
Exception Classes
● ArithmeticException
● ArrayIndexOutOfBoundsException
● NullPointerException
● IOException
These help identify the type of problem that occurred during execution.
Uncaught Exceptions
If exceptions are not caught using try-catch, the program terminates abruptly. These are
called uncaught exceptions.
Handling Exception
Handled using try, catch, and finally blocks.
Example:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("Always executes");
}
User-defined Exception
Example:
🔁 Multithreaded Programming
Java Thread Model
Java supports multithreading – the ability to execute multiple threads simultaneously. Each
thread is a lightweight sub-process, managed by the Java Virtual Machine.
Runnable Interface
Thread Class
Thread class provides methods to start, run, sleep, and control threads. Can be extended to
create a thread.
Example:
Thread Creation
States of a thread:
Thread Scheduling
Java uses priority-based thread scheduling. Thread priorities range from 1 (min) to 10 (max).
Synchronization ensures only one thread accesses a resource at a time. Used to prevent race
conditions.
Example:
Deadlock happens when two or more threads wait for each other forever.
Inter-thread Communication
thread.join();
Methods like suspend(), resume(), and stop() are deprecated due to unsafe behavior.
Use a flag variable or interrupt() for safe thread control.
Example: