Trainer : Mr. Nantha
Day 9
Understanding inheritance is essential because it helps in code reuse, structured design, and real-world modeling.
What is Inheritance in Java?
Inheritance is a mechanism in Java where a child class acquires the properties and behaviors of a parent class.
It represents an IS-A relationship.
Example
Developer is an Employee
SeniorDeveloper is a Developer
This allows classes to reuse existing code instead of rewriting it.
Types of Inheritance in Java
Java supports several inheritance structures:
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance
Let’s explore each one.
Single Inheritance
Definition
Single inheritance occurs when one child class inherits from one parent class.
Structure
Parent → Child
Example
Animal → Dog
Code Example
class Animal {
void eat() {
System.out.println("Animal eats food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Explanation
Dog inherits the eat() method from Animal.
Dog also has its own method bark().
This improves code reusability.
Multiple Inheritance
Definition
Multiple inheritance occurs when one child class inherits from more than one parent class.
Structure
Parent1
\
Child
/
Parent2
Example (Conceptual)
Father
\
Child
/
Mother
Problem – Ambiguity Issue
If both parents have the same method:
void work()
Java cannot determine:
Should the child use Father.work()
or
Mother.work()?
This confusion is called the Ambiguity Problem.
Important Rule
🚫 Java does NOT support multiple inheritance using classes.
Example that causes an error:
class Child extends Father, Mother { }
This will produce a compile-time error.
But Java Allows Multiple Inheritance via Interfaces
Example:
interface Camera {
void takePhoto();
}
interface MusicPlayer {
void playMusic();
}
class Smartphone implements Camera, MusicPlayer {
public void takePhoto() {
System.out.println("Taking Photo");
}
public void playMusic() {
System.out.println("Playing Music");
}
}
Here a class can implement multiple interfaces safely.
Hierarchical Inheritance
Definition
Hierarchical inheritance occurs when multiple child classes inherit from a single parent class.
Structure
Vehicle
/ \
Car Bike
Example Code
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("Car drives");
}
}
class Bike extends Vehicle {
void ride() {
System.out.println("Bike rides");
}
}
Explanation
Both Car and Bike inherit the start() method from Vehicle.
Benefits:
Code reuse
Better organization
Common functionality centralized
Multilevel Inheritance
Definition
Multilevel inheritance occurs when a class inherits from another class, and then another class inherits from that class.
Structure
Grandparent → Parent → Child
Example
Animal → Dog → Puppy
Code Example
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
}
Execution
Puppy p = new Puppy();
p.eat(); // from Animal
p.bark(); // from Dog
p.weep(); // from Puppy
Output
Animal eats
Dog barks
Puppy weeps
Why Multilevel Inheritance is Powerful
✔ Reusability across multiple layers
✔ Represents real-world hierarchies
✔ Makes code scalable
✔ Easy to extend functionality
Real-world example:
Person → Employee → Developer → SeniorDeveloper
Hybrid Inheritance
Definition
Hybrid inheritance is a combination of two or more inheritance types.
Example:
Multilevel + Hierarchical
Structure Example
Vehicle
/ \
Car Bike
|
ElectricCar
Important Rule
Java does not support hybrid inheritance using classes directly because it may involve multiple inheritance, which causes ambiguity.
However, it can be achieved using interfaces.
Key Concepts Learned Today
✔ Types of inheritance in Java
✔ Why Java avoids multiple inheritance with classes
✔ Ambiguity problem
✔ Hierarchical inheritance structure
✔ Multilevel inheritance
✔ Introduction to method overriding
✔ Real-world class hierarchy design
Top comments (0)