Java Inheritance Types – Complete Guide with Examples


Learn all types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance, with detailed examples and best practices in object-oriented programming.

Inheritance Types in Java – Complete Detailed Tutorial

Inheritance is a key feature of Object-Oriented Programming (OOP).

It allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass).

This promotes code reuse and modularity.

1. Advantages of Inheritance

  1. Code Reusability: Use existing methods and fields in new classes
  2. Method Overriding: Customize inherited methods
  3. Extensibility: Easy to extend programs
  4. Data Hiding: Parent class can restrict access using access modifiers

2. Syntax of Inheritance


class Parent {
// fields and methods
}

class Child extends Parent {
// inherits fields and methods from Parent
}
  1. extends keyword is used to inherit a class
  2. Java supports single, multilevel, hierarchical inheritance
  3. Multiple inheritance with classes is not supported (avoids ambiguity)
  4. Interfaces can provide multiple inheritance

3. Types of Inheritance in Java

3.1 Single Inheritance

  1. A child class inherits from a single parent class

Example – Single Inheritance:


class Animal {
void eat() {
System.out.println("Animal eats 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(); // inherited from Animal
d.bark(); // own method
}
}

Output:


Animal eats food
Dog barks

3.2 Multilevel Inheritance

  1. A class inherits from a child class, forming a chain

Example – Multilevel Inheritance:


class Animal {
void eat() {
System.out.println("Animal eats food");
}
}

class Mammal extends Animal {
void walk() {
System.out.println("Mammal walks");
}
}

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

public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat(); // from Animal
d.walk(); // from Mammal
d.bark(); // from Dog
}
}

Output:


Animal eats food
Mammal walks
Dog barks

3.3 Hierarchical Inheritance

  1. Multiple child classes inherit from a single parent class

Example – Hierarchical Inheritance:


class Animal {
void eat() {
System.out.println("Animal eats 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();
d.eat();
d.bark();

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

Output:


Animal eats food
Dog barks
Animal eats food
Cat meows

3.4 Multiple Inheritance (Through Interfaces)

  1. Java does not support multiple inheritance with classes to avoid ambiguity
  2. Interfaces allow multiple inheritance

Example – Multiple Inheritance via Interface:


interface A {
void methodA();
}

interface B {
void methodB();
}

class C implements A, B {
public void methodA() {
System.out.println("Method A");
}
public void methodB() {
System.out.println("Method B");
}
}

public class Main {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

Output:


Method A
Method B

3.5 Hybrid Inheritance (Combination)

  1. Combination of multiple inheritance types
  2. Achieved using interfaces to avoid class-based multiple inheritance

Example:


interface X {
void showX();
}

interface Y {
void showY();
}

class Parent {
void showParent() {
System.out.println("Parent class method");
}
}

class Child extends Parent implements X, Y {
public void showX() {
System.out.println("Interface X method");
}
public void showY() {
System.out.println("Interface Y method");
}
}

public class Main {
public static void main(String[] args) {
Child c = new Child();
c.showParent();
c.showX();
c.showY();
}
}

Output:


Parent class method
Interface X method
Interface Y method

4. Summary of Inheritance Types

TypeDescription
SingleOne child inherits from one parent
MultilevelChain of inheritance (grandparent → parent → child)
HierarchicalMultiple children inherit from one parent
Multiple (via Interface)One class implements multiple interfaces
Hybrid (Combination)Combination of above types using classes and interfaces

Key Points:

  1. Promotes code reuse and modularity
  2. Avoids redundancy
  3. Method overriding works with inheritance