Multilevel inheritance is a type of inheritance in which a class (child class) inherits from another class (parent class), and that parent class can itself inherit from another class. This forms a chain of inheritance. In multilevel inheritance, a derived class can have its own derived classes, allowing for a hierarchy of classes.
In Java, multilevel inheritance is implemented using the extends
keyword. Here’s a basic syntax structure:
class Grandparent {
// Members of Grandparent
}
class Parent extends Grandparent {
// Members of Parent
}
class Child extends Parent {
// Members of Child
}
Let’s illustrate multilevel inheritance with an example involving a hierarchy of vehicles.
// Grandparent class
class Vehicle {
void start() {
System.out.println("Vehicle is starting.");
}
}
Explanation: The Vehicle
class is the grandparent class that defines a general behavior, start()
, applicable to all vehicles.
// Parent class
class Car extends Vehicle {
void drive() {
System.out.println("Car is being driven.");
}
}
Explanation: The Car
class extends the Vehicle
class, inheriting its start()
method and adding its own method, drive()
.
// Child class
class SportsCar extends Car {
void accelerate() {
System.out.println("Sports car is accelerating.");
}
}
Explanation: The SportsCar
class extends the Car
class, inheriting both start()
and drive()
methods while adding its specific behavior, accelerate()
.
public class Main {
public static void main(String[] args) {
// Create an instance of SportsCar
SportsCar sportsCar = new SportsCar();
// Call methods from various levels
sportsCar.start(); // Inherited from Vehicle
sportsCar.drive(); // Inherited from Car
sportsCar.accelerate(); // Specific to SportsCar
}
}
When you run the Main
class, you will see the following output
Vehicle is starting.
Car is being driven.
Sports car is accelerating.
Vehicle Class: This is the grandparent class that provides a method start()
, defining a common action for all vehicles.
Car Class: This is the parent class that extends Vehicle
, inheriting its method start()
. It introduces the drive()
method, which is specific to cars.
SportsCar Class: This is the child class that extends Car
, inheriting both the start()
method from Vehicle
and the drive()
method from Car
. It adds its own method, accelerate()
.
Main Class: In the main
method, an instance of SportsCar
is created. It can access methods from all three classes in the hierarchy, demonstrating the benefits of multilevel inheritance.
Multilevel inheritance is commonly used in various domains:
Software Development: It is useful in modeling complex systems where multiple levels of abstractions are necessary, such as GUI frameworks where a base component class can have buttons, labels, and more as subclasses.
Gaming: In game development, you may have a base class Character
, with subclasses like Player
and Enemy
, and then further subclasses like Warrior
and Mage
, creating a rich hierarchy of behaviors and properties.
Database Systems: In systems where entities share common attributes but also have specialized behaviors, such as Person
(grandparent), Employee
(parent), and Manager
(child), multilevel inheritance can simplify design