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:

  1. Chain of Inheritance: In multilevel inheritance, a class can inherit from another class that is already a subclass. This creates a chain of inheritance.

  2. Code Reusability: Each subclass can inherit methods and fields from its superclass, promoting code reuse.

  3. Hierarchical Organization: It allows for better organization of classes based on shared behaviors and attributes.

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

  1. Superclass Animal:

    • This class has a method sound() that can be inherited by subclasses.
  2. Subclass Dog:

    • This class extends the Animal class and inherits its method. It also defines its own method bark().
  3. Subclass Puppy:

    • This class extends the Dog class, inheriting its properties and methods. It defines its own method weep().
  4. Main Class:

    • In the main method, we create an instance of Puppy called myPuppy.
    • We can call the inherited method sound() from the Animal class, the bark() method from the Dog class, and the weep() method defined in the Puppy class.

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:

  1. Code Reusability: Subclasses can reuse code from their parent classes, reducing redundancy.

  2. Logical Organization: Helps to logically organize classes and their relationships in a clear hierarchy.

  3. Enhanced Flexibility: Subclasses can override methods from their parent classes, allowing for tailored behavior.

Limitations of Multilevel Inheritance:

  1. Complexity: The hierarchy can become complex with multiple levels, making it harder to manage and understand.

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