2-๐Ÿงฌ Types of Inheritance in Java โ€“ Complete Guide with Examples, Code & Common Mistakes

๐ŸŒŸ Introduction

In the previous article of this inheritance series, we learned the basic concept of inheritance in Javaโ€”how a class can inherit properties and behaviors from another class using the extends keyword.

Inheritance allows programmers to:

  • Reuse existing code
  • Reduce redundancy
  • Build structured applications
  • Represent real-world relationships in programs

However, inheritance relationships between classes can be structured in different ways. Depending on how classes are connected in a hierarchy, inheritance can take several forms.

These forms are called Types of Inheritance.

Understanding these types is very important because they help developers:

  • Design better class hierarchies
  • Avoid common design problems
  • Understand why Java supports some inheritance patterns but restricts others

In this article, we will cover:

  • All types of inheritance
  • Java-supported inheritance structures
  • Examples with Java code
  • Important rules and limitations
  • Common beginner mistakes

By the end of this article, you will have a clear understanding of how inheritance structures work in Java.


๐Ÿง  What Are Types of Inheritance?

Types of inheritance refer to the different ways classes can be related using inheritance.

In object-oriented programming, inheritance structures create hierarchies of classes.

There are five commonly discussed types of inheritance:

1๏ธโƒฃ Single Inheritance
2๏ธโƒฃ Multilevel Inheritance
3๏ธโƒฃ Hierarchical Inheritance
4๏ธโƒฃ Multiple Inheritance
5๏ธโƒฃ Hybrid Inheritance

However, Java supports only three of them directly with classes.

Supported in Java:

โœ” Single Inheritance
โœ” Multilevel Inheritance
โœ” Hierarchical Inheritance

Not supported with classes:

โŒ Multiple Inheritance
โŒ Hybrid Inheritance

Let us understand each type step by step.


๐ŸŒฑ Single Inheritance

๐Ÿงพ Definition

Single inheritance occurs when one child class inherits from exactly one parent class.

Structure:

Parent Class
โ†“
Child Class

This is the simplest form of inheritance.


๐Ÿ’ป Example of Single Inheritance

class Animal {

    void eat() {
        System.out.println("Animals eat food");
    }

}

class Dog extends Animal {

    void bark() {
        System.out.println("Dog barks");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();

        d.eat();
        d.bark();

    }

}

๐Ÿ”Ž Code Explanation

First class:

class Animal

This is the parent class.


Method inside parent class:

void eat()

This method represents a behavior common to animals.


Child class declaration:

class Dog extends Animal

The Dog class inherits from Animal.

Because of inheritance, Dog automatically gets access to the eat() method.


Dog-specific method:

void bark()

This method belongs only to the Dog class.


Object creation:

Dog d = new Dog();

This creates a Dog object.


Method calls:

d.eat();
d.bark();

The Dog object can call:

  • Parent method โ†’ eat()
  • Child method โ†’ bark()

๐Ÿงพ Output

Animals eat food
Dog barks

๐ŸŒฟ Multilevel Inheritance

๐Ÿงพ Definition

Multilevel inheritance occurs when a class inherits from another class, which itself inherits from another class.

Structure:

Grandparent
โ†“
Parent
โ†“
Child

๐Ÿ’ป Example of Multilevel Inheritance

class Animal {

    void eat() {
        System.out.println("Animals eat food");
    }

}

class Dog extends Animal {

    void bark() {
        System.out.println("Dog barks");
    }

}

class Puppy extends Dog {

    void weep() {
        System.out.println("Puppy weeps");
    }

}

public class Main {

    public static void main(String[] args) {

        Puppy p = new Puppy();

        p.eat();
        p.bark();
        p.weep();

    }

}

๐Ÿ”Ž Code Explanation

Hierarchy:

Animal โ†’ Dog โ†’ Puppy

Explanation:

  • Dog inherits from Animal
  • Puppy inherits from Dog

Therefore the Puppy class gets access to:

โœ” eat() from Animal
โœ” bark() from Dog
โœ” weep() from Puppy


๐Ÿงพ Output

Animals eat food
Dog barks
Puppy weeps

โš ๏ธ Common Beginner Confusion

Many students think:

Puppy inherits only Dog methods.

This is incorrect.

Puppy inherits both Dog and Animal methods.


๐ŸŒณ Hierarchical Inheritance

๐Ÿงพ Definition

Hierarchical inheritance occurs when multiple child classes inherit from the same parent class.

Structure:

        Animal
/ \
Dog Cat

๐Ÿ’ป Example of Hierarchical Inheritance

class Animal {

    void eat() {
        System.out.println("Animals eat food");
    }

}

class Dog extends Animal {

    void bark() {
        System.out.println("Dog barks");
    }

}

class Cat extends Animal {

    void meow() {
        System.out.println("Cat meows");
    }

}

public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();
        Cat c = new Cat();

        d.eat();
        d.bark();

        c.eat();
        c.meow();

    }

}

๐Ÿ”Ž Explanation

Here:

  • Dog inherits from Animal
  • Cat also inherits from Animal

Both classes share the eat() method from the parent class.


๐Ÿงพ Output

Animals eat food
Dog barks
Animals eat food
Cat meows

๐Ÿšซ Multiple Inheritance (Not Supported in Java Classes)

๐Ÿงพ Definition

Multiple inheritance occurs when a child class inherits from more than one parent class.

Structure:

Parent1     Parent2
\ /
Child

Example (not allowed in Java):

class A {
}class B {
}class C extends A, B {
}

This will cause a compile-time error.


๐Ÿง  Why Java Does Not Support Multiple Inheritance

The main reason is the Diamond Problem.

Example structure:

      A
/ \
B C
\ /
D

If both B and C override a method from A, then class D cannot determine:

Which method should it inherit?

This ambiguity is called the Diamond Problem.

To avoid this issue, Java does not allow multiple inheritance with classes.

However, Java supports multiple inheritance through interfaces, which we will discuss later in the series.


๐Ÿ”ท Hybrid Inheritance

Hybrid inheritance is a combination of two or more inheritance types.

Example:

Single + Hierarchical

Java does not support hybrid inheritance with classes, but it can be implemented using interfaces.


โš ๏ธ Common Mistakes Students Make

โŒ Mistake 1: Forgetting the extends keyword

Without extends, inheritance does not occur.


โŒ Mistake 2: Thinking parent classes can access child methods

Parent classes cannot access child class members.

Example:

Animal cannot call bark()

โŒ Mistake 3: Confusing multilevel inheritance with multiple inheritance

Multilevel:

A โ†’ B โ†’ C

Multiple:

A
\
C
/
B

โŒ Mistake 4: Accessing private variables from child class

Private members are not accessible directly in child classes.


๐Ÿง  Quick Summary Table

TypeSupported in Java Classes
Singleโœ” Yes
Multilevelโœ” Yes
Hierarchicalโœ” Yes
MultipleโŒ No
HybridโŒ No

๐Ÿ“Œ Key Points to Remember

โœ” Inheritance helps create class hierarchies
โœ” Java supports three types directly
โœ” Multiple inheritance causes ambiguity
โœ” Interfaces allow multiple inheritance in Java
โœ” Understanding inheritance structures helps design better programs


๐Ÿ Conclusion

Inheritance is a powerful concept in Java that allows developers to build programs using hierarchical relationships between classes. It enables code reuse, better organization, and improved maintainability.

In this article, we explored the different types of inheritance, including:

  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Multiple Inheritance
  • Hybrid Inheritance

We also understood why Java does not support multiple inheritance with classes and how this restriction prevents ambiguity in programs.

Understanding these inheritance types is essential for designing clean and efficient object-oriented systems.

In the next article of this inheritance series, we will explore:

๐Ÿš€ Method Overriding in Java โ€“ How a child class modifies the behavior of a parent class method.

This concept is extremely important because it introduces runtime polymorphism, which is one of the most powerful features of Java.

Mastering it will take your understanding of object-oriented programming to the next level.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *