DEV Community

Cover image for πŸš€ Day 9 of My Java Automation Journey – Types of Inheritance
bala d kaveri
bala d kaveri

Posted on • Edited on

πŸš€ Day 9 of My Java Automation Journey – Types of Inheritance

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

❌ 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Example:

Employee β†’ Developer β†’ Senior_Dev
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” Explanation

salary β†’ Instance variable

Welcome() β†’ Method
Enter fullscreen mode Exit fullscreen mode

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");
    }
}
Enter fullscreen mode Exit fullscreen mode

πŸ” What is happening here?

Developer extends Employee
Inherits salary
Inherits Welcome() method
Adds new variable:
Designation
Overrides Welcome() method

πŸ‘‰ This is called Method Overriding (next topic πŸ˜‰)

πŸ”Ή Step 3: Senior_Dev (Child of Developer)

public class Senior_Dev extends Developer
Enter fullscreen mode Exit fullscreen mode

Now the inheritance chain becomes:

Employee
   ↓
Developer
   ↓
Senior_Dev
Enter fullscreen mode Exit fullscreen mode

πŸ”Ž Execution Flow of Senior_Dev

Senior_Dev Senior1 = new Senior_Dev();
Enter fullscreen mode Exit fullscreen mode

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();
Enter fullscreen mode Exit fullscreen mode

Calls Developer’s Welcome() method

Output:

Welcome 2
Enter fullscreen mode Exit fullscreen mode

Because method overriding happens.

Senior1.Designation = "Senior Developer";

Enter fullscreen mode Exit fullscreen mode

Changes inherited variable from Developer

Senior1.salary = 10000;
Enter fullscreen mode Exit fullscreen mode
  • Accessing Employee variable
  • Even though it's 2 levels above

Final Output:

Welcome 2
Senior Developer
Salary 10000
Enter fullscreen mode Exit fullscreen mode

🎯 Why Multilevel Inheritance is Powerful

βœ… Reusability across multiple layers
βœ… Real-world modeling
βœ… Code organization
βœ… Easy expansion

Real-world example:

Person β†’ Employee β†’ Developer β†’ Senior Developer
Enter fullscreen mode Exit fullscreen mode

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)