Java Documentation

2.3 Types of Inheritance

How Java models inheritance between classes, which forms are supported by classes, and how interfaces provide multiple inheritance of type.

Overview

Inheritance is a fundamental object-oriented programming (OOP) concept that allows a class to acquire properties and behaviors from another class. Java supports several inheritance models, each serving different design needs:

  • Single Inheritance - One child class inherits from one parent class
  • Multilevel Inheritance - Chain of inheritance through multiple levels
  • Hierarchical Inheritance - Multiple child classes inherit from one parent class
Note: Java does not support multiple inheritance for classes to avoid complexity and ambiguity, but provides interfaces as an alternative to achieve multiple inheritance of type.

Supported Class Inheritance Types

1. Single Inheritance

A single child class inherits from one parent class. This is the simplest and most common form of inheritance.

SingleInheritance.java
class A {
    int length;
    void setLength(int l) {
        length = l;
    }
}

class B extends A {
    int breadth;
    void setBreadth(int breadth) {
        this.breadth = breadth;
    }
    
    void area() {
        int a = length * breadth;
        System.out.println("Area = "+a);
    }
}

public class SingleInheritance {
    public static void main (String[] args) {
        B obj = new B();
        obj.setLength(5);
        obj.setBreadth(4);
        obj.area();
        
    }
}
Reference & full codeView on GitHub

Output:

Area = 20
Key Points: The child class can access all non-private members of the parent class. Only one direct superclass is allowed per class.

2. Multilevel Inheritance

A chain of inheritance where a class inherits from another class, which in turn inherits from another class. This creates a multi-level hierarchy.

MultilevelInheritance.java
class A {
    int length;
    void setLength(int l) {
        length = l;
    }
}

class B extends A {
    int breadth;
    void setBreadth(int breadth) {
        this.breadth = breadth;
    }
    
    void area() {
        int a = length * breadth;
        System.out.println("Area = "+a);
    }
}
class C extends B {
    int height;
    void setHeight(int height) {
        this.height = height;
    }
    
    void volume() {
        int v = length * breadth * height;
        System.out.println("volume = "+v);
    }
}

public class MultilevelInheritance {
    public static void main (String[] args) {
        C obj = new C();
        obj.setLength(5);
        obj.setBreadth(4);
        obj.setHeight(3);
        obj.area();
        obj.volume();
        
    }
}
Reference & full codeView on GitHub

Output:

Area = 20
volume = 60
Key Points: Creates a hierarchical chain. Each level adds new functionality while inheriting from the previous level.

3. Hierarchical Inheritance

Multiple child classes inherit from a single parent class. This is useful when multiple classes share common behavior.

HierarchicalInheritance.java
class Circle {
    double radius;
    void setRadius(double r) {
        radius = r;
    }
}

class CricleArea extends Circle {
    void area() {
        System.out.println("Area = "+3.14*radius*radius);
    }
}

class CriclePerimeter extends Circle {
    void perimeter() {
        System.out.println("Perimeter = "+2*3.14*radius);
    }
}

public class HierarchicalInheritance {
    public static void main (String[] args) {
        CricleArea obj1 = new CricleArea();
        obj1.setRadius(5.2);
        obj1.area();
        
        CriclePerimeter obj2 = new CriclePerimeter();
        obj2.setRadius(3.2);
        obj2.perimeter();
    }
}
Reference & full codeView on GitHub

Output:

Area = 84.90560000000002
Perimeter = 20.096000000000004
Key Points: Promotes code reusability. All child classes share common behavior from the parent class while implementing their own specific behavior.

Why Java Restricts Multiple Inheritance of Classes

Multiple inheritance (one class directly extending more than one base class) is not supported in Java. This intentional design choice addresses several issues:

The Diamond Problem

When a class inherits from two classes that have a method with the same signature, it becomes ambiguous which parent's method should be called.

DiamondProblemExample.java
class A {
    void show() { System.out.println("A"); }
}

class B {
    void show() { System.out.println("B"); }
}

// This would cause ambiguity - which show() to inherit?
// class C extends A, B { } // NOT ALLOWED IN JAVA

Design Benefits of This Restriction

  • Simplicity: Reduces complexity in the language design
  • Predictability: Makes code behavior more predictable and easier to understand
  • Clean Hierarchy: Encourages cleaner, more maintainable class hierarchies
  • Promotes Composition: Encourages using composition over inheritance for code reuse

Multiple Inheritance via Interfaces

While Java doesn't support multiple inheritance for classes, it does support multiple inheritance for interfaces. A class can implement multiple interfaces, providing multiple inheritance of type without the complexity of multiple implementation inheritance.

MultipleInheritance.java
interface College {
    String c_name = "NCIT College";
    void showInfo();
}

class Student {
    String s_name;
    String faculty;

    Student (String a, String b) {
        s_name = a;
        faculty = b;
    }
}

class StudentDetails extends Student implements College {
    public StudentDetails(String a, String b) {
        super(a,b);
    }
    public void showInfo() {
        System.out.println("Student Name: "+s_name);
        System.out.println("College Name: "+c_name);
    }
}

public class MultipleInheritance {
    public static void main (String[] args) {
        StudentDetails obj = new StudentDetails("Utsab Adhikari", "BEIT");
        obj.showInfo();
    }
} 
Reference & full codeView on GitHub

Output:

Student Name: Utsab Adhikari
College Name: NCIT College
Key Points: The StudentDetails class inherits from the Student class and implements the College interface, demonstrating multiple inheritance of type.

Advantages of Interface-based Multiple Inheritance

  • Avoids the diamond problem
  • Promotes loose coupling
  • Enables polymorphism across unrelated types
  • Supports design patterns like Adapter and Strategy

When to Use Interfaces

  • When we need to define a contract
  • When unrelated classes need common behavior
  • When we want to separate API from implementation
  • When we need multiple inheritance of type