Java supports several types of inheritance, although multiple inheritance of classes is not
allowed. Instead, Java uses interfaces to achieve polymorphism and shared contracts, which
is often considered a form of multiple inheritance of type.
The four main types of inheritance conceptually recognized in Java are: Single, Multilevel,
Hierarchical, and Hybrid (achieved via Interfaces).
Type Description Key Mechanism
Single A class inherits from only one extends keyword
direct superclass.
Multilevel A class inherits from a second extends keyword
class, which in turn inherits
from a third class (A \rightarrow
B \rightarrow C).
Hierarchical Multiple subclasses inherit from extends keyword
a single superclass.
Hybrid A combination of two or more extends and implements
types of inheritance. (Achieved keywords
using interfaces in Java.)
Multiple A class inherits directly from NOT supported for classes
more than one superclass.
1. Single Inheritance
In single inheritance, a class inherits from only one direct superclass. This is the simplest and
most common form of inheritance.
Example
class Animal { // Superclass
void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal { // Subclass
void bark() {
System.out.println("Dog is barking.");
}
}
// Usage:
// Dog d = new Dog();
// d.eat(); // Inherited from Animal
// d.bark();
2. Multilevel Inheritance
In multilevel inheritance, a class acts as a base class for another derived class, which in turn
acts as a base class for a third derived class (A \rightarrow B \rightarrow C).
Example
class Vehicle { // Grandparent
void speed() {
System.out.println("Vehicle moves.");
}
}
class Car extends Vehicle { // Parent
void type() {
System.out.println("Car is a four-wheeler.");
}
}
class Sedan extends Car { // Child
void luxury() {
System.out.println("Sedan is a luxury car.");
}
}
// Usage:
// Sedan s = new Sedan();
// s.speed(); // Inherited from Vehicle
// s.type(); // Inherited from Car
// s.luxury();
3. Hierarchical Inheritance
In hierarchical inheritance, a single superclass is inherited by multiple subclasses.
Example
class Shape { // Superclass
void draw() {
System.out.println("Drawing a shape.");
}
}
class Circle extends Shape { // Subclass 1
void circleMethod() { }
}
class Square extends Shape { // Subclass 2
void squareMethod() { }
}
// Usage:
// Circle c = new Circle();
// c.draw();
// Square s = new Square();
// s.draw();
4. Hybrid Inheritance (Achieved with Interfaces)
Hybrid inheritance is a combination of two or more types of inheritance. Since multiple
inheritance of classes is forbidden in Java, hybrid inheritance involving multiple class paths is
achieved using interfaces. This allows a class to inherit type and contract from multiple
sources.
Example
In this example, we combine Hierarchical (A is the parent of B and C) and the functionality of
Multiple Inheritance (D extends B and implements A and C's features/contracts).
// Interface (Achieves a form of 'multiple inheritance')
interface Printable {
void print();
}
// Class hierarchy
class A {
void methodA() { System.out.println("Method A"); }
}
class B extends A { // Single Inheritance
void methodB() { System.out.println("Method B"); }
}
class C implements Printable { // Single Inheritance + Interface
public void print() {
System.out.println("Printing from C");
}
}
// Hybrid structure: D extends B (Multilevel) and implements Printable
(Multiple Type Inheritance)
class D extends B implements Printable {
// D now has methods from B, A, and Printable
public void print() {
System.out.println("Printing from D");
}
// D also has methodA() and methodB()
}
// Usage:
// D obj = new D();
// obj.methodA(); // from A
// obj.print(); // from Printable (via D's implementation)
Type NOT Supported: Multiple Inheritance of Classes
Java does not support a class inheriting directly from more than one superclass (e.g., class C
extends A, B).
Reason for Exclusion
The primary reason is to avoid the "Deadly Diamond of Death" (DOD) problem, which causes
ambiguity. If classes A and B both had a method with the same signature (e.g., calculate()) and
class C inherited from both, the compiler wouldn't know which implementation of calculate() to
use when C calls it. Interfaces prevent this because historically they only declared method
signatures, forcing the inheriting class (C) to provide the single implementation.