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:

  1. Multiple Subclasses: A single superclass can have multiple subclasses, each inheriting the properties and methods of the superclass.

  2. Code Reusability: Subclasses can reuse the methods and fields defined in the superclass, reducing code duplication.

  3. Clear Structure: It establishes a clear relationship among classes, making it easier to understand and manage.

  4. 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:

  1. Superclass Animal:

    • This class has methods sound() and eat(). All subclasses will inherit these methods.
  2. Subclass Dog:

    • This class extends the Animal class and overrides the sound() method to provide a specific implementation for dogs. It also defines a method fetch().
  3. Subclass Cat:

    • This class extends the Animal class and overrides the sound() method to provide a specific implementation for cats. It also defines a method scratch().
  4. Main Class:

    • In the main method, instances of Dog and Cat are created.
    • The overridden sound() methods are called, showcasing polymorphism.
    • Both subclasses can also call the inherited eat() method from the Animal superclass.

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:

  1. Code Reusability: Multiple subclasses can share common methods and properties defined in a single superclass.

  2. Logical Organization: Provides a clear organizational structure for related classes, enhancing maintainability.

  3. Ease of Understanding: Makes it easier to understand relationships between classes.

Limitations of Hierarchical Inheritance:

  1. Increased Complexity: The class hierarchy can become complex if not managed properly, making it difficult to understand.

  2. 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.