Types of Inheritance
Single, multilevel, hierarchical
5 min read
Types of Inheritance in Java
Java supports several inheritance patterns with classes, but has restrictions to avoid complexity.
Inheritance Types
- Single: One child, one parent
- Multilevel: Chain: A ā B ā C
- Hierarchical: Multiple children, one parent
- Multiple: NOT supported with classes (use interfaces)
- Hybrid: Combination (limited in Java)
ā ļø Java Restriction: A class can only extend ONE class. Multiple inheritance is only possible through interfaces!
Code Examples
Single inheritance - simplest form
java
1// Single Inheritance: One parent, one child
2public class Animal {
3 public void eat() {
4 System.out.println("Eating...");
5 }
6}
7
8public class Dog extends Animal { // Dog has ONE parent
9 public void bark() {
10 System.out.println("Barking...");
11 }
12}
13
14Dog dog = new Dog();
15dog.eat(); // From Animal
16dog.bark(); // From DogMultilevel inheritance chain
java
1// Multilevel Inheritance: Chain of inheritance
2public class Animal {
3 public void eat() {
4 System.out.println("Eating...");
5 }
6}
7
8public class Mammal extends Animal {
9 public void breathe() {
10 System.out.println("Breathing air...");
11 }
12}
13
14public class Dog extends Mammal {
15 public void bark() {
16 System.out.println("Barking...");
17 }
18}
19
20// Chain: Animal ā Mammal ā Dog
21Dog dog = new Dog();
22dog.eat(); // From Animal (grandparent)
23dog.breathe(); // From Mammal (parent)
24dog.bark(); // From Dog (self)
25
26// instanceof checks
27System.out.println(dog instanceof Dog); // true
28System.out.println(dog instanceof Mammal); // true
29System.out.println(dog instanceof Animal); // true
30System.out.println(dog instanceof Object); // trueHierarchical inheritance - multiple children
java
1// Hierarchical Inheritance: Multiple children, one parent
2public class Vehicle {
3 protected String brand;
4
5 public void start() {
6 System.out.println("Vehicle starting");
7 }
8}
9
10public class Car extends Vehicle {
11 public void drive() {
12 System.out.println("Car driving on road");
13 }
14}
15
16public class Motorcycle extends Vehicle {
17 public void ride() {
18 System.out.println("Motorcycle riding");
19 }
20}
21
22public class Truck extends Vehicle {
23 public void haul() {
24 System.out.println("Truck hauling cargo");
25 }
26}
27
28// All inherit from Vehicle
29Vehicle v1 = new Car();
30Vehicle v2 = new Motorcycle();
31Vehicle v3 = new Truck();
32
33// Polymorphic array
34Vehicle[] vehicles = {v1, v2, v3};
35for (Vehicle v : vehicles) {
36 v.start(); // All can start
37}Multiple inheritance through interfaces
java
1// Multiple Inheritance - Via Interfaces Only!
2
3// ā This is NOT allowed:
4// class Child extends Parent1, Parent2 {} // ERROR!
5
6// ā Multiple interfaces ARE allowed:
7interface Flyable {
8 void fly();
9}
10
11interface Swimmable {
12 void swim();
13}
14
15interface Walkable {
16 void walk();
17}
18
19// Duck can implement multiple interfaces
20public class Duck implements Flyable, Swimmable, Walkable {
21 @Override
22 public void fly() {
23 System.out.println("Duck flying");
24 }
25
26 @Override
27 public void swim() {
28 System.out.println("Duck swimming");
29 }
30
31 @Override
32 public void walk() {
33 System.out.println("Duck walking");
34 }
35}
36
37// Can extend class AND implement interfaces
38public class Bird {
39 public void eat() {
40 System.out.println("Bird eating");
41 }
42}
43
44public class Eagle extends Bird implements Flyable {
45 @Override
46 public void fly() {
47 System.out.println("Eagle soaring");
48 }
49}Use Cases
- Single: Simple extension of functionality
- Multilevel: Creating specialized hierarchies
- Hierarchical: Common base for variants
- Interfaces: Multiple behavior contracts
- Hybrid: Complex domain models
- Framework class hierarchies
Common Mistakes to Avoid
- Trying to extend multiple classes
- Too deep inheritance hierarchies
- Using inheritance when composition fits better
- Diamond problem misunderstanding
- Confusing interface implementation with inheritance
- Tight coupling through inheritance