C

Core Java tutorial for beginners

Clean • Professional

Java - Inheritance and Its Categories (With Examples & Diagram)

6 minute

Inheritance and Its Categories in Java

Inheritance is one of the four core concepts of Object-Oriented Programming (OOP) in Java.

It allows one class to acquire properties and behaviors (fields and methods) of another class — helping you reuse, extend, and maintain code more easily.

What is Inheritance in Java?

Inheritance in Java is a mechanism where one class (child/subclass) derives the properties and methods of another class (parent/superclass).

It is implemented using the extends keyword.

class Parent {
    // fields and methods
}

class Child extends Parent {
    // inherits Parent’s members
}

Why Use Inheritance?

BenefitDescription
Code ReusabilityAvoid writing the same code multiple times.
Improved MaintainabilityUpdate logic in one place (parent class).
ExtensibilityEasily add new features via subclasses.
PolymorphismAllows dynamic behavior using method overriding.

Real-Life Example

A Vehicle can be the parent class, and Car, Bike, Truck can be subclasses.

class Vehicle {
    void start() {
        System.out.println("Vehicle is starting...");
    }
}

class Car extends Vehicle {
    void honk() {
        System.out.println("Car is honking!");
    }
}

public class InheritanceExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.start(); // inherited method
        car.honk();  // own method
    }
}

Output:

Vehicle is starting...
Car is honking!

Syntax of Inheritance

class SubclassName extends SuperclassName {
    // body of subclass
}

Example:

class Dog extends Animal { }

Here,

  • Animal → Parent Class (Superclass)
  • Dog → Child Class (Subclass)

Key Terms

TermMeaning
Superclass (Parent Class)The class whose members are inherited.
Subclass (Child Class)The class that inherits the superclass members.
extends keywordUsed to define inheritance.
super keywordRefers to parent class members.

Types (Categories) of Inheritance in Java

Java supports several categories of inheritance, depending on how classes are related.

learn code with durgesh images

1. Single Inheritance

One class inherits from another single class.

class A {
    void showA() { System.out.println("Class A"); }
}
class B extends A {
    void showB() { System.out.println("Class B"); }
}
public class SingleInheritance {
    public static void main(String[] args) {
        B obj = new B();
        obj.showA();
        obj.showB();
    }
}

Output:

Class A
Class B

Used When: One class needs the functionality of one parent class.


2. Multilevel Inheritance

A chain of inheritance (one class inherits from another derived class).

class A {
    void methodA() { System.out.println("A class"); }
}
class B extends A {
    void methodB() { System.out.println("B class"); }
}
class C extends B {
    void methodC() { System.out.println("C class"); }
}
public class MultilevelExample {
    public static void main(String[] args) {
        C obj = new C();
        obj.methodA();
        obj.methodB();
        obj.methodC();
    }
}

Output:

A class
B class
C class

Used When: You want features to build layer-by-layer.


3. Hierarchical Inheritance

Multiple subclasses inherit from a single parent class.

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 HierarchicalExample {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.eat();
        d.bark();

        Cat c = new Cat();
        c.eat();
        c.meow();
    }
}

Output:

Animals eat food
Dog barks
Animals eat food
Cat meows

Used When: Different classes share common properties/methods.


4. Multiple Inheritance (via Interfaces)

Java does not support multiple inheritance with classes to avoid ambiguity (the “diamond problem”).

However, it supports multiple inheritance using interfaces.

interface A {
    void display();
}
interface B {
    void print();
}
class C implements A, B {
    public void display() { System.out.println("Display method"); }
    public void print() { System.out.println("Print method"); }
}
public class MultipleInheritanceExample {
    public static void main(String[] args) {
        C obj = new C();
        obj.display();
        obj.print();
    }
}

Output:

Display method
Print method

Used When: You need a class to follow multiple behavior contracts.


5. Hybrid Inheritance

A combination of two or more types of inheritance. Not directly supported by Java (to avoid ambiguity), but can be achieved using interfaces.

Example: Hierarchical + Multiple via interfaces.


Points to Remember

  • Java supports Single, Multilevel, Hierarchical, and Multiple (via interfaces) inheritance.
  • Constructors are not inherited, but can be called using super().
  • Private members of the parent class are not accessible in the child class.
  • super keyword helps access parent methods, variables, and constructors.
  • Avoid deep inheritance chains for cleaner design.

Real-Life Example

class Employee {
    int salary = 40000;
}

class Developer extends Employee {
    int bonus = 10000;
}

public class RealLifeExample {
    public static void main(String[] args) {
        Developer d = new Developer();
        System.out.println("Salary: " + d.salary);
        System.out.println("Bonus: " + d.bonus);
    }
}

Output:

Salary: 40000
Bonus: 10000

 

Article 0 of 0