Home  >  Article  >  Java  >  Hierarchical Inheritance in Java

Hierarchical Inheritance in Java

WBOY
WBOYOriginal
2024-08-30 15:26:03987browse

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.

ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock Tests

Hierarchical Inheritance in Java

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:

Hierarchical Inheritance in Java

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.<em><i>out</i></em>.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:

Hierarchical Inheritance in Java

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:

Hierarchical Inheritance in Java

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.

The above is the detailed content of Hierarchical Inheritance in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn