Java Multilevel inheritance
Multilevel inheritance in Java is a type of inheritance where a class is derived from another derived class, creating a chain of inheritance. In this hierarchy, each subclass can inherit the properties and methods from its immediate superclass, and it can also act as a superclass for another class. This allows for a structured and organized way of building classes, promoting code reuse and a clear relationship among classes.
Key Features of Multilevel Inheritance:
Chain of Inheritance: In multilevel inheritance, a class can inherit from another class that is already a subclass. This creates a chain of inheritance.
Code Reusability: Each subclass can inherit methods and fields from its superclass, promoting code reuse.
Hierarchical Organization: It allows for better organization of classes based on shared behaviors and attributes.
Flexibility: Subclasses can override methods from their superclasses, allowing for more flexible behavior.
Example of Multilevel Inheritance
Let’s illustrate multilevel inheritance with an example involving an Animal
superclass, a Dog
subclass, and a Puppy
subclass.
Step 1: Define the Superclass
// Superclass
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
Step 2: Define the First Subclass
// First subclass
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
Step 3: Define the Second Subclass
// Second subclass
class Puppy extends Dog {
void weep() {
System.out.println("Puppy whimpers");
}
}
Step 4: Use the Classes in the Main Method
// Main class
public class Main {
public static void main(String[] args) {
// Create an instance of Puppy
Puppy myPuppy = new Puppy();
// Call methods from the Puppy class
myPuppy.sound(); // Inherited from Animal
myPuppy.bark(); // Inherited from Dog
myPuppy.weep(); // Puppy’s own method
}
}
Explanation of the Example:
Superclass
Animal
:- This class has a method
sound()
that can be inherited by subclasses.
- This class has a method
Subclass
Dog
:- This class extends the
Animal
class and inherits its method. It also defines its own methodbark()
.
- This class extends the
Subclass
Puppy
:- This class extends the
Dog
class, inheriting its properties and methods. It defines its own methodweep()
.
- This class extends the
Main Class:
- In the
main
method, we create an instance ofPuppy
calledmyPuppy
. - We can call the inherited method
sound()
from theAnimal
class, thebark()
method from theDog
class, and theweep()
method defined in thePuppy
class.
- In the
Output of the Example:
When you run the above code, the output will be:
Animal makes a sound
Dog barks
Puppy whimpers
Advantages of Multilevel Inheritance:
Code Reusability: Subclasses can reuse code from their parent classes, reducing redundancy.
Logical Organization: Helps to logically organize classes and their relationships in a clear hierarchy.
Enhanced Flexibility: Subclasses can override methods from their parent classes, allowing for tailored behavior.
Limitations of Multilevel Inheritance:
Complexity: The hierarchy can become complex with multiple levels, making it harder to manage and understand.
Increased Coupling: Changes in the superclass can affect all its subclasses, leading to tight coupling.
Summary:
- Multilevel inheritance in Java allows for a chain of classes where a subclass can inherit from another subclass, creating a hierarchy.
- It promotes code reusability, logical organization, and flexibility while allowing subclasses to inherit and override methods from their parent classes.