Java Multilevel Inheritance

Introduction

Multilevel inheritance in Java is a type of inheritance where a class is derived from another class, which in turn is derived from another class. This creates a chain of inheritance. Multilevel inheritance helps to model a more hierarchical structure and promotes code reusability by allowing subclasses to inherit features from multiple levels of a class hierarchy.

Table of Contents

  1. What is Multilevel Inheritance?
  2. Benefits of Multilevel Inheritance
  3. Implementing Multilevel Inheritance in Java
  4. The super Keyword
  5. Method Overriding
  6. Conclusion

1. What is Multilevel Inheritance?

Multilevel inheritance occurs when a class is derived from a class that is also derived from another class. This creates a chain of inheritance in which each derived class inherits properties and behaviors from its immediate superclass and all its ancestors.

Real-World Example

Consider the classification of living organisms:

  • At the top level, there is a general category like “Animal”.
  • A more specific category is “Mammal”, which inherits properties of “Animal”.
  • Even more specific is “Dog”, which inherits properties from “Mammal” and indirectly from “Animal”.

This hierarchical classification helps organize entities and reuse common features at each level.

2. Benefits of Multilevel Inheritance

  • Code Reusability: Allows reuse of code across multiple levels of the class hierarchy.
  • Hierarchical Classification: Helps in organizing classes in a hierarchical structure.
  • Extendable: Allows for extending the functionality of existing classes.

3. Implementing Multilevel Inheritance in Java

In Java, multilevel inheritance is implemented by chaining multiple extends keywords.

Syntax:

class Superclass {
    // fields and methods
}

class Subclass extends Superclass {
    // additional fields and methods
}

class SubSubclass extends Subclass {
    // additional fields and methods
}

Example: Multilevel Inheritance

In this example, the Manager class inherits from the Employee class, which in turn inherits from the Person class. This demonstrates multilevel inheritance, where each level extends the functionality of the previous level.

// Superclass
public class Person {
    String name;

    // Constructor
    public Person(String name) {
        this.name = name;
    }

    public void display() {
        System.out.println("Name: " + name);
    }
}

// Intermediate subclass
public class Employee extends Person {
    int employeeId;

    // Constructor
    public Employee(String name, int employeeId) {
        super(name); // Call to superclass constructor
        this.employeeId = employeeId;
    }

    public void display() {
        super.display(); // Call to superclass method
        System.out.println("Employee ID: " + employeeId);
    }
}

// Final subclass
public class Manager extends Employee {
    String department;

    // Constructor
    public Manager(String name, int employeeId, String department) {
        super(name, employeeId); // Call to superclass constructor
        this.department = department;
    }

    public void display() {
        super.display(); // Call to superclass method
        System.out.println("Department: " + department);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating an object of the Manager class
        Manager manager = new Manager("Alice", 101, "HR");
        // Displaying manager details
        manager.display();
    }
}

Output:

Name: Alice
Employee ID: 101
Department: HR

4. The super Keyword

The super keyword in Java is used to refer to the immediate superclass of a subclass. It can be used to:

  • Call the superclass’s constructor.
  • Access superclass methods.
  • Access superclass fields.

Example: Using super Keyword in Multilevel Inheritance

public class Animal {
    String name;

    // Constructor
    public Animal(String name) {
        this.name = name;
    }

    public void display() {
        System.out.println("Animal: " + name);
    }
}

public class Mammal extends Animal {
    String type;

    // Constructor
    public Mammal(String name, String type) {
        super(name); // Call to superclass constructor
        this.type = type;
    }

    public void display() {
        super.display(); // Call to superclass method
        System.out.println("Type: " + type);
    }
}

public class Dog extends Mammal {
    String breed;

    // Constructor
    public Dog(String name, String type, String breed) {
        super(name, type); // Call to superclass constructor
        this.breed = breed;
    }

    public void display() {
        super.display(); // Call to superclass method
        System.out.println("Breed: " + breed);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy", "Mammal", "Golden Retriever");
        myDog.display();
    }
}

Output:

Animal: Buddy
Type: Mammal
Breed: Golden Retriever

5. Method Overriding

Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. The overridden method in the subclass must have the same signature as the method in the superclass.

Example: Method Overriding in Multilevel Inheritance

public class Animal {
    public void sound() {
        System.out.println("This animal makes a sound.");
    }
}

public class Mammal extends Animal {
    @Override
    public void sound() {
        System.out.println("This mammal makes a sound.");
    }
}

public class Dog extends Mammal {
    @Override
    public void sound() {
        System.out.println("The dog barks.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.sound(); // Calls the overridden method in Dog class
    }
}

Output:

The dog barks.

6. Conclusion

Multilevel inheritance in Java allows a class to inherit properties and behaviors from another class, which in turn inherits from another class. This promotes code reusability and helps in organizing classes in a hierarchical structure.

By understanding and implementing multilevel inheritance, you can create more modular, maintainable, and efficient Java applications. The super keyword and method overriding are essential aspects of inheritance that enable subclasses to extend and customize the behavior of their superclasses.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top