Hierarchical Inheritance – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 19 Sep 2022 16:15:17 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Hierarchical Inheritance – Programmerbay https://programmerbay.com 32 32 Hierarchical Inheritance in Java with program Example https://programmerbay.com/hierarchical-inheritance-in-java-with-program-example/ https://programmerbay.com/hierarchical-inheritance-in-java-with-program-example/#respond Mon, 19 Sep 2022 16:15:17 +0000 https://programmerbay.com/?p=9057 Hierarchical inheritance is a type of inheritance in which two or more classes inherit a single parent class. In this, multiple classes acquire properties of the same superclass. The classes that inherit all the attributes or behaviour are known as child classes or subclass or derived classes. The class that is inherited by others is known as a superclass or parent class or base class. For instance, class B, class C, and class D inherit the same class name.

Hierarchical Inheritance in Java

Syntax :

class A {

}

class B extends A {

}

class C extends A {

}

Hierarchical Inheritance Programs in Java

In order to implement hierarchical inheritance, we need to ensure at least two classes inherit the same parent class.

Example 1.  Java Program to implement Hierarchical Inheritance

Program:

public class A {
    public void display() {
        System.out.println("I am a method from class A");
    }
}

class B extends A {
    public void print() {
        System.out.println("I am a method from class B");
    }
}

class C extends A {
    public void show() {
        System.out.println("I am a method from class C");
    }
}

class D extends A {
    public void outPut() {
        System.out.println("I am a method from class D");
    }

    public static void main(String[] args) {
        B objB = new B();
        C objC = new C();
        D objD = new D();
        objB.display();
        objC.display();
        objD.display();
    }
}

Output:

I am a method from class A
I am a method from class A
I am a method from class A

Explanation:

  1. In the above example, we are having four classes namely A, B, C and D
    Class B, C, and D are inheriting the same class named A.
  2. We’ve declared and initialized objects of B, C and D to demonstrate that all has the access of display() functionality of parent class A.
  3. Lastly, we are simply printing output from display() method using B, C, and D object

Example 2.  Java Program to calculate the salary of an Employee using Hierarchical Inheritance

Program:

public class Employee {
    int baseSalary;
    Employee(){
        this.baseSalary = 50000;
    }
}

class TempEmployee extends Employee{
    float incrementPercent;

    TempEmployee(){
        this.incrementPercent = 0.1f;
    }

}

class PermanentEmployee extends Employee{
    float incrementPercent;

    PermanentEmployee(){
        this.incrementPercent = 0.2f;
    }

    public static void main(String[] args) {
        TempEmployee tempEmployee = new TempEmployee();
        PermanentEmployee permanentEmployee = new PermanentEmployee();

        System.out.println("Total salary of Temporary Employee :: "+ ((tempEmployee.baseSalary * tempEmployee.incrementPercent) + tempEmployee.baseSalary));
        System.out.println("Total salary of Permanent Employee :: "+ ((permanentEmployee.baseSalary * permanentEmployee.incrementPercent) + permanentEmployee.baseSalary));
    }
}

Output:

Total salary of Temporary Employee :: 55000.0
Total salary of Permanent Employee :: 60000.0

Explanation:

  1. In the above example, we are having three classes named Employee, TempEmployee, and PermanentEmployee
  2. Class TempEmployee and PermanentEmployee are extending the Employee class. As a result, both classes have access to the baseSalary field of the Employee class.
  3. We are simply creating objects of TempEmployee and PermanentEmployee classes. Both objects have initialized with some default bonus percentage value via the constructor
  4. Based on bonusPercentage and basicSalary fields, we are calculating and printing the salary of both types of employees.

]]>
https://programmerbay.com/hierarchical-inheritance-in-java-with-program-example/feed/ 0