Java Hierarchical inheritance
Hierarchical inheritance in Java is a type of inheritance where multiple subclasses inherit from a single superclass. This allows the subclasses to share common behavior and attributes defined in the superclass while having their unique implementations. Hierarchical inheritance promotes code reuse and simplifies class organization, as multiple classes can be derived from a common base class.
Key Features of Hierarchical Inheritance:
Multiple Subclasses: A single superclass can have multiple subclasses, each inheriting the properties and methods of the superclass.
Code Reusability: Subclasses can reuse the methods and fields defined in the superclass, reducing code duplication.
Clear Structure: It establishes a clear relationship among classes, making it easier to understand and manage.
Flexible Method Overriding: Each subclass can provide its own implementation of methods inherited from the superclass.
Example of Hierarchical Inheritance
Let’s illustrate hierarchical inheritance with an example involving an Animal
superclass and two subclasses: Dog
and Cat
.
Step 1: Define the Superclass
// Superclass
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
void eat() {
System.out.println("Animal eats");
}
}
Step 2: Define the First Subclass
// First subclass
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
void fetch() {
System.out.println("Dog fetches the ball");
}
}
Step 3: Define the Second Subclass
// Second subclass
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
void scratch() {
System.out.println("Cat scratches the post");
}
}
Step 4: Use the Classes in the Main Method
// Main class
public class Main {
public static void main(String[] args) {
// Create instances of Dog and Cat
Dog myDog = new Dog();
Cat myCat = new Cat();
// Call methods from the Dog class
myDog.sound(); // Output: Dog barks
myDog.eat(); // Inherited from Animal
myDog.fetch(); // Dog’s own method
System.out.println(); // Just for better readability
// Call methods from the Cat class
myCat.sound(); // Output: Cat meows
myCat.eat(); // Inherited from Animal
myCat.scratch(); // Cat’s own method
}
}
Explanation of the Example:
Superclass
Animal
:- This class has methods
sound()
andeat()
. All subclasses will inherit these methods.
- This class has methods
Subclass
Dog
:- This class extends the
Animal
class and overrides thesound()
method to provide a specific implementation for dogs. It also defines a methodfetch()
.
- This class extends the
Subclass
Cat
:- This class extends the
Animal
class and overrides thesound()
method to provide a specific implementation for cats. It also defines a methodscratch()
.
- This class extends the
Main Class:
- In the
main
method, instances ofDog
andCat
are created. - The overridden
sound()
methods are called, showcasing polymorphism. - Both subclasses can also call the inherited
eat()
method from theAnimal
superclass.
- In the
Output of the Example:
When you run the above code, the output will be:
Dog barks Animal eats Dog fetches the ball Cat meows Animal eats Cat scratches the post
Advantages of Hierarchical Inheritance:
Code Reusability: Multiple subclasses can share common methods and properties defined in a single superclass.
Logical Organization: Provides a clear organizational structure for related classes, enhancing maintainability.
Ease of Understanding: Makes it easier to understand relationships between classes.
Limitations of Hierarchical Inheritance:
Increased Complexity: The class hierarchy can become complex if not managed properly, making it difficult to understand.
Tight Coupling: Changes to the superclass can affect all subclasses, leading to tight coupling between classes.
Summary:
- Hierarchical inheritance in Java allows multiple subclasses to inherit from a single superclass, promoting code reuse and establishing clear relationships among classes.
- It simplifies the class structure while enabling subclasses to override inherited methods, resulting in flexible and organized code.