On Day 9, I learned about:
β Different Types of Inheritance in Java
Yesterday we discussed Single Inheritance.
Today I explored all inheritance types and understood how Java handles them internally.
π What is Inheritance?
Inheritance is a mechanism where:
A child class acquires the properties and behaviors of a parent class.
It represents an:
π IS-A Relationship
Example:
Developer is an Employee
Senior_Dev is a Developer
π Types of Inheritance in Java
1οΈβ£ Single Inheritance
2οΈβ£ Multiple Inheritance
3οΈβ£ Hierarchical Inheritance
4οΈβ£ Multilevel Inheritance
5οΈβ£ Hybrid Inheritance
Letβs understand each one clearly π
1οΈβ£ Single Inheritance
π Definition
When one child class inherits from one parent class.
Structure:
Parent β Child
Example:
Employee β Developer
2οΈβ£ Multiple Inheritance
π Definition
When one child class tries to inherit from more than one parent class.
Structure (Not allowed in Java classes):
Mom
β
Child
β
Dad
β Problem: Ambiguity Issue
If both parent classes have the same method:
void work()
Then Java cannot decide:
Which parent's method should the child use?
This is called:
β Ambiguity Problem
π« Important
Java does NOT support multiple inheritance using classes.
Butβ¦
β
It supports multiple inheritance via Interfaces (we will learn later).
If we try:
class Child extends Mom, Dad
π It throws a compile-time error.
3οΈβ£ Hierarchical Inheritance
π Definition
When multiple child classes inherit from the same parent class.
Structure:
Employee
/ \
Developer Testing
Example:
Developer extends Employee
Testing extends Employee
Both share common properties like:
salary
empId
common methods
This improves reusability.
4οΈβ£ Multilevel Inheritance (Important Today)
π Definition
When a class inherits from another class, and then another class inherits from that class.
Structure:
Grandparent β Parent β Child
Example:
Employee β Developer β Senior_Dev
This is called Multilevel Inheritance.
π» Now Letβs Understand the Code
πΉ Step 1: Employee (Parent Class)
public class Employee {
int salary;
void Welcome() {
System.out.println("Welcome");
}
}
π Explanation
salary β Instance variable
Welcome() β Method
Acts as base class
Contains common properties
πΉ Step 2: Developer (Child of Employee)
p
ublic class Developer extends Employee {
String Designation = "Devloper";
void Welcome() {
System.out.println("Welcome 2");
}
}
π What is happening here?
Developer extends Employee
Inheritssalary
InheritsWelcome()method
Adds new variable:
Designation
OverridesWelcome()method
π This is called Method Overriding (next topic π)
πΉ Step 3: Senior_Dev (Child of Developer)
public class Senior_Dev extends Developer
Now the inheritance chain becomes:
Employee
β
Developer
β
Senior_Dev
π Execution Flow of Senior_Dev
Senior_Dev Senior1 = new Senior_Dev();
What happens in memory?
The object contains:
salary (from Employee)
Designation (from Developer)
Welcome() (overridden version from Developer)
Senior_Dev part
Code Execution
Senior1.Welcome();
Calls Developerβs Welcome() method
Output:
Welcome 2
Because method overriding happens.
Senior1.Designation = "Senior Developer";
Changes inherited variable from Developer
Senior1.salary = 10000;
- Accessing Employee variable
- Even though it's 2 levels above
Final Output:
Welcome 2
Senior Developer
Salary 10000
π― Why Multilevel Inheritance is Powerful
β
Reusability across multiple layers
β
Real-world modeling
β
Code organization
β
Easy expansion
Real-world example:
Person β Employee β Developer β Senior Developer
5οΈβ£ Hybrid Inheritance
π Definition
Combination of two or more types of inheritance.
Example:
- Hierarchical + Multilevel
β Java does not support Hybrid inheritance using classes directly (because it may involve multiple inheritance).
But it can be achieved using interfaces.
π§ Important Concepts Learned Today
β Types of inheritance
β Why multiple inheritance is not supported
β Ambiguity problem
β Hierarchical inheritance structure
β Multilevel inheritance
β Method overriding preview
β Real-world class hierarchy
π Day 9 Key Takeaways
Java supports:
Single
Multilevel
Hierarchical
Java does NOT support:
Multiple inheritance (with classes)
Java supports multiple inheritance:
Via Interfaces only
π¨βπ« Trainer: Nantha from Payilagam
π€ A Small Note
I used ChatGPT to help structure and refine this blog while ensuring the concepts remain aligned with my trainerβs explanations.

Top comments (0)