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
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.
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();
}
}Output:
Area = 20
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.
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();
}
}
Output:
Area = 20
volume = 60
3. Hierarchical Inheritance
Multiple child classes inherit from a single parent class. This is useful when multiple classes share common behavior.
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();
}
}Output:
Area = 84.90560000000002
Perimeter = 20.096000000000004
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.
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 JAVADesign 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.
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();
}
} Output:
Student Name: Utsab Adhikari
College Name: NCIT College
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