Home > Java > javaTutorial > Hybrid Inheritance in Java

Hybrid Inheritance in Java

王林
Release: 2024-08-30 15:26:24
Original
1172 people have browsed it

Inheritance is a property of the JAVA language where in the functions and members of one class can be inherited and used by other classes. This helps in advancing the usage of clean code and reusability. JAVA does provide different kinds of inheritance but multiple inheritances. To overcome this shortcoming, JAVA uses hybrid inheritance with the help of interfaces. Hybrid inheritance is a combination of different types of inheritances (specifically single and multilevel inheritance).

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How Hybrid Inheritance works in Java?

For example, there are different classes named “A”, “B”,”C” and “D”. Suppose class “A” and “B” extends class “C”. Also, another class, “D,” extends class “A”. Here class “A” is a parent class for child class “D” and is also a child class for parent class “C”.  This can be demonstrated via a diagram below:

Hybrid Inheritance in Java

The syntax for hybrid inheritance is not different from normal inheritance. The only additional thing to notice here is that more than one inheritance is implemented in the same program to achieve the result. This functionality is to have the advantage to add up functionalities on top of the existing ones and thus reducing the work of coders to write code from scratch.

Example of Hybrid Inheritance in Java

Explanation: The below code snippet demonstrates the working of hybrid inheritance in JAVA. In the below, code four classes are declared with the function name display(). Display()  function returns void but internally calls println() function to print the string in the output screen. Println() function is a standard function by JAVA using a java input-output library to print a string in the output screen. It takes a string as a parameter captured under semicolons.

The main class here is test4. The main class is the one in which the main function is declared. Here main class is declared as:

public static void main(String args[])
Copy after login

This main function takes a string as an input parameter and returns void. This function is by default public and is the first function which is called when the program is executed. The main function has an object of the main class “test4” created. The object of the class test4 is “object”. This object is calling a function display. The string in the display function of test4 is hence printed.

Here the thing to notice is that the test4 class inherits the property of the class test1. Test1, in turn, inherits the property of the test3. Test 2 as well as inherits the property of the test3. Hence there are two classes inheriting test 3, which are: test 1 and test2. So here we can see different types of inheritance like a single and hierarchal inheritance.  The above case is an example of a hierarchal and single inheritance.

Class test1 and test2 extends class test3 → Hierarchical inheritance

Class test4 extends class test1 → Single inheritance

The below code can be used to understand the working.

Code:

//Class test3 which works as a parent class for other two child classes.
class test3
{
// this function is used to class print function. This function name is used in other class as well but with other string in it. This feature in JAVA is called as function overloading.
public void display()
{
System.out.println("the program control is in class test3");
//JAVA standard function to print the string in the output screen.
}
}
//Here test1 class in extending test3’s function. This is hierarchal inheritance.
class test1 extends test3
{
//JAVA function overloading. Same function name is used again in the other class.
public void display()
{
System.out.println("the program control is in class test1");
}
}
//Here test2 class in extending test3’s function. This is hierarchal inheritance.
class test2 extends test3
{
public void display()
{
System.out.println("the program control is in class test2");
}
}
//This is the main class which is extending test1. Such inheritance is called a single inheritance.
class Main extends test1
{
public void display()
{
System.out.println("The program control is in class test4 and It is an example \n to demonstrate the working of hybrid inheritance in JAVA.");
}
public static void main(String args[]){
//”object” objet is created by instantiating test4 class. His class is then used to call display() function from class test4.
Main object = new Main();
object.display();
}
}
Copy after login

Output:

Since the main class in test4, so java program should be saved from the name “test4.java’.

In CMD, we need to navigate to the path where this file is saved. In this case, I saved the file on the desktop. Then compile the program by typing “JAVAC test4.JAVA”. Once it is compiled, a new file can be seen in the desktop names “test4.class”. This is the executable file of JAVA. It is saved by the system with the same name as the main file name, but only the designation changes from .java to .class. Then we can execute the file using the command “JAVA test4” and find the output result.

Hybrid Inheritance in Java

Conclusion

Hybrid inheritance is a great way to achieve the functionalities of two different inheritance types in one. His feature provided by JAVA enables code reusability. JAVA does not support multiple inheritances, but this functionality can be relished with the help of interfaces along with inheritance. This is very useful when we try to have standard practices like code reusability, polymorphism, and encapsulation in our code to match up to industry standards. These practices ensure easy code maintenance over a longer period of time.

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

Related labels:
source:php
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template