Java Hierarchical Inheritance

Introduction

Hierarchical inheritance is a type of inheritance in Java where multiple subclasses inherit from a single superclass. This type of inheritance is useful when you want to share common functionality among different classes while allowing each subclass to have its own specific features.

Table of Contents

  1. What is Hierarchical Inheritance?
  2. Benefits of Hierarchical Inheritance
  3. Implementing Hierarchical Inheritance in Java
  4. Method Overriding
  5. The super Keyword
  6. Real-World Analogy
  7. Example: Hierarchical Inheritance
  8. Conclusion

1. What is Hierarchical Inheritance?

Hierarchical inheritance occurs when multiple subclasses inherit from a single superclass. This allows the subclasses to share common functionality defined in the superclass while also defining their own unique behaviors.

2. Benefits of Hierarchical Inheritance

  • Code Reusability: Common functionality can be defined in the superclass and reused by all subclasses.
  • Simplicity: Simplifies code by organizing shared functionality in a single place.
  • Flexibility: Each subclass can add its own specific functionality in addition to the inherited features.

3. Implementing Hierarchical Inheritance in Java

Hierarchical inheritance is implemented using the extends keyword.

Syntax:

class Superclass {
    // fields and methods
}

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

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

4.() 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

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

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

public class Cat extends Animal {
    @Override
    public void sound() {
        System.out.println("The cat meows.");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.sound(); // Calls the overridden method in Dog class
        myCat.sound(); // Calls the overridden method in Cat class
    }
}

Output:

The dog barks.
The cat meows.

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

public class Animal {
    String name;

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

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

public class Dog extends Animal {
    String breed;

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

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

public class Cat extends Animal {
    String color;

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

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

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy", "Golden Retriever");
        Cat myCat = new Cat("Whiskers", "White");

        myDog.display();
        myCat.display();
    }
}

Output:

Animal: Buddy
Breed: Golden Retriever
Animal: Whiskers
Color: White

6. Real-World Analogy

Consider a company where:

  • The superclass is Employee which contains common fields like name and salary.
  • The subclasses are Manager and Developer which inherit from Employee and add their own specific features like department for Manager and programmingLanguage for Developer.

7. Example: Hierarchical Inheritance

Let’s create a more detailed example of hierarchical inheritance in Java.

Example: Hierarchical Inheritance

// Superclass
public class Employee {
    String name;
    int employeeId;

    // Constructor
    public Employee(String name, int employeeId) {
        this.name = name;
        this.employeeId = employeeId;
    }

    public void displayDetails() {
        System.out.println("Name: " + name + ", Employee ID: " + employeeId);
    }
}

// Subclass 1
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 displayDetails() {
        super.displayDetails(); // Call to superclass method
        System.out.println("Department: " + department);
    }
}

// Subclass 2
public class Developer extends Employee {
    String programmingLanguage;

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

    public void displayDetails() {
        super.displayDetails(); // Call to superclass method
        System.out.println("Programming Language: " + programmingLanguage);
    }
}

public class Main {
    public static void main(String[] args) {
        // Creating objects of Manager and Developer classes
        Manager manager = new Manager("Alice", 101, "HR");
        Developer developer = new Developer("Bob", 102, "Java");

        // Displaying details
        manager.displayDetails();
        developer.displayDetails();
    }
}

Output:

Name: Alice, Employee ID: 101
Department: HR
Name: Bob, Employee ID: 102
Programming Language: Java

In this example, the Manager and Developer classes inherit from the Employee class, demonstrating hierarchical inheritance. Both subclasses share common fields and methods from the superclass and add their own specific features.

8. Conclusion

Hierarchical inheritance in Java allows multiple subclasses to inherit from a single superclass. This promotes code reusability and helps in organizing classes in a hierarchical structure. By understanding and implementing hierarchical 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