What is Hierarchical Inheritance in Java?
Hierarchical Inheritance in Java is one of the types of inheritance in java. Inheritance is one of the important features of an Object-Oriented programming system (oops). An inheritance is a mechanism in which one class inherits or acquires all the other class’s attributes and behaviours. The class inherits the attributes and behaviors called a parent or super or base class, and the class inherits the attributes and behaviors are called child or derived class. In Hierarchical Inheritance, the multiple child classes inherit the single class or the single class is inherited by multiple child class. The use of inheritance in Java is for the reusability of code and for the dynamic polymorphism (method overriding).
How does it Work in Java?
We can understand the Hierarchical Inheritance more clearly with the help of the below diagram.
As in the above example figure, ClassB and ClassC inherit the same or single class ClassA. So the ClassA variables and methods are reuse in both classes, ClassB and ClassC. The above diagram shows that more than one child classes have the same parent class, so this type of inheritance is called Hierarchical Inheritance.
Syntax of Single Inheritance in Java:
class Subclassname1 extends Superclassname
{
// variables and methods
}
Syntax of Hierarchical Inheritance in Java:
class Subclassname1 extends Superclassname
{
// variables and methods
}
class Subclassname2 extends Superclassname
{
// variables and methods
}
The “extends” meaning is to increase the functionality. The extends keyword indicates inheritance; that is, we are making a new class that derives from an existing class.
Examples of Hierarchical Inheritance in Java
Following are the different examples:
Example #1
Example of Hierarchical Inheritance in Java to inherit a variable from the superclass. Next, we write the Java code to understand the hierarchical inheritance to inherit a variable from the superclass with the following example.
Code:
package P1;
class Employee{
float salary = 40000;
}
class PermanentEmp extends Employee{
double hike = 0.5;
}
class TemporaryEmp extends Employee{
double hike = 0.35;
}
public class HerInheritanceDemo
{
public static void main(String args[]){
PermanentEmp p = new PermanentEmp();
TemporaryEmp t = new TemporaryEmp();
// All objects of inherited classes can access the variable of class Employee
System.out.println("Permanent Employee salary is :" +p.salary);
System.out.println("Hike for Permanent Employee is:" +p.hike);
System.out.println("Temporary Employee salary is :" +t.salary);
System.out.println("Hike for Temporary Employee is :" +t.hike);
}
}
Output:
As in the above code, PermanentEmp class and TemporaryEmp classes are the subclass, and Employee is the superclass and objects of these subclasses are accessing the variable of the superclass, which shows the hierarchal inheritance concept or feature in Java.
Example #2
Example of Hierarchical Inheritance in Java to inherit the method from the superclass. Next, we write the Java code to understand this in Java more clearly with the following example.
Code:
package P1;
class Employee{
float salary = 40000;
void dispSalary()
{
System.out.println("The Employee salary is :" +salary);
}
}
class PermanentEmp extends Employee{
double hike = 0.5;
void incrementSalary()
{
System.out.println("The Permanent Employee incremented salary is :" +(salary+(salary * hike)));
}
}
class TemporaryEmp extends Employee{
double hike = 0.35;
void incrementSalary()
{
System.out.println("The Temporary Employee incremented salary is :" +(salary+(salary * hike)));
}
}
public class HerInheritanceDemo
{
public static void main(String args[]){
PermanentEmp p = new PermanentEmp();
TemporaryEmp t = new TemporaryEmp();
// All objects of inherited classes can access the method of class Employee
p.dispSalary();
p.incrementSalary();
t.dispSalary();
t.incrementSalary();
}
}
Output:
As in the above code, PermanentEmp class and TemporaryEmp classes are the subclass, and Employee is the superclass and objects of these subclasses are calling to the method of the superclass, which shows the hierarchal inheritance concept or feature in Java.
Example #3
For example to call the method of the superclass with a super keyword. Next, we rewrite the above Java code to understand the super keyword’s working in it more clearly with the following example.
Code:
package P1;
class Employee{
float salary = 40000;
void dispSalary()
{
System.out.println("The Employee salary is :" +salary);
}
}
class PermanentEmp extends Employee{
double hike = 0.5;
void incrementSalary()
{
super.dispSalary();
System.out.println("The Permanent Employee incremented salary is :" +(salary+(salary * hike)) );
}
}
class TemporaryEmp extends Employee{
double hike = 0.35;
void incrementSalary()
{
super.dispSalary();
System.out.println("The Temporary Employee incremented salary is :" +(salary+(salary * hike)) );
}
}
public class HerInheritanceDemo
{
public static void main(String args[]){
PermanentEmp p = new PermanentEmp();
TemporaryEmp t = new TemporaryEmp();
// All objects of inherited classes can access the variable of class Employee
p.incrementSalary();
t.incrementSalary();
}
}
Output:
As in the above code, PermanentEmp class and TemporaryEmp classes are the subclasses, and Employee is the superclass, and inside the subclasses methods, the superclass method is calling with prefixing by “super” keyword. The super keyword is a reference variable in Java, which is used to reference variables and methods of the parent class object. In the main method, objects of subclasses call to their own method, which again shows the concept or feature in Java.
Conclusion
Inheritance is a feature in which one class inherits all the attributes and behaviors of the other class. One of the types of inheritance in Java is Hierarchical Inheritance in Java. In Hierarchical Inheritance, more than one class inherits attributes and methods from a single class.
Recommended Articles
This is a guide to Hierarchical Inheritance in Java. Here we discuss the Introduction and examples of hierarchical inheritance in Java along with code implementation. You may also look at the following articles to learn more –