×

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a new class by deriving from an existing one using the inheritance mechanism. The following four types of inheritance are supported by Java:

  • Single Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Only hybrid inheritance in Java will be covered in this section, along with appropriate examples and various hybrid inheritance implementation strategies.

Hybrid Inheritance

Hybrid (mixture) often refers to something that is composed of multiple elements. A hybrid inheritance in Java is made up of two or more different inheritance types. The fundamental goal of hybrid inheritance is to modularize the code into simple classes. It also offers code reuse. The following combinations can be used to achieve hybrid inheritance:

  • Multiple and Single Inheritance (not supported but can be achieved through an interface)
  • Hierarchical and Multilevel Inheritance
  • Single and Hierarchical Inheritance

How does hybrid inheritance work in Java?

There are four classes, let's say A, B, C, and D. Assume that class "C" is extended by class "A" and "B." Additionally, class "A" is extended by class "D". In this case, class "A" serves as both a parent class for class "D" and a child class for class "C." The illustration helps us to comprehend the example.

Hybrid Inheritance in Java

Single and Multiple Inheritance

By combining single inheritance with multiple inheritances in the following Java program, we were able to accomplish hybrid inheritance (through interfaces).

We used the human body as an example in our software because it has many distinct functions, like walking, talking, dancing, and eating. Male or female human bodies are both possible. Therefore, the HumanBody class has two interfaces, Male and Female. The features of the HumanBody class, which represents a single inheritance, are inherited by both the parent class and the child class.

Let's say a Male and a Female decide to have a child. Therefore, the functionality of the Male and Female interfaces is also inherited by the Child class. It is an illustration of multiple inheritances.

Filename: Child.java

public class Child extends HumanBody implements Male, Female  
{  
public void show()  
{  
System.out.println("Implementation of show() method defined in interfaces Male and Female");  
}    
public void displayChild()  
{  
System.out.println("Method defined inside Child class");  
}   
public static void main(String args[])  
{  
Child obj = new Child();  
System.out.println("Implementation of Hybrid Inheritance in Java");  
obj.show();  
obj.displayChild();  
}  
}  
class HumanBody  
{  
public void displayHuman()  
{  
System.out.println("Method defined inside HumanBody class");  
}  
}  
interface Male  
{  
public void show();  
}  
interface Female  
{  
public void show();  
}  

Output

Hybrid Inheritance in Java

Using Multilevel and Hierarchical Inheritance

Grandfather is shown as a super class in the following diagram. The GrandFather class's properties are passed down to the Father class as a single inheritance is represented by Father and Grandfather. The Son and Daughter classes also inherit from the Father class. Father now serves as the Son and Daughter's parent class. These classes represent hierarchical inheritance. Together, they signify hybrid inheritance.

Hybrid Inheritance in Java

Let's use a Java program to implement the hybrid inheritance approach.

Filename: Daughter.java

//inherits Father's properties    
public class Daughter extends Father    
{    
public void showD()    
{    
System.out.println("She is daughter.");    
}    
public static void main(String args[])    
{    
//Daughter obj = new Daughter();    
//obj.show();    
Son obj = new Son();  
obj.showS();  // Accessing Son class method  
obj.showF();  // Accessing Father class method  
obj.showG();  // Accessing GrandFather class method  
Daughter obj2 = new Daughter();  
obj2.showD();  // Accessing Daughter class method  
obj2.showF();    // Accessing Father class method  
obj2.showG();   // Accessing GrandFather class method  
}    
}  
//parent class    
class GrandFather    
{    
public void showG()    
{    
System.out.println("He is grandfather.");    
}    
}    
//inherits GrandFather's properties    
class Father extends GrandFather    
{    
public void showF()    
{    
System.out.println("He is father.");    
}    
}    
//inherits Father's properties    
class Son extends Father    
{    
public void showS()    
{    
System.out.println("He is son.");    
}    
}

Output

Hybrid Inheritance in Java

Hierarchical and Single Inheritance

By combining hierarchical and single inheritance in the Java program that follows, we were able to accomplish hybrid inheritance.

Here, classes A and B extend to class C, which stands for hierarchical inheritance. class D extends class A, which stands for single inheritance.

Filename: D.java

public class D extends A  
{  
public void disp()  
{  
System.out.println("D");  
}  
public static void main(String args[])  
{  
D obj = new D();  
obj.disp();  
}  
} 
class C  
{  
public void disp()  
{  
System.out.println("C");  
}  
}  
class A extends C  
{  
public void disp()  
{  
System.out.println("A");  
}  
}  
class B extends C  
{  
public void disp()  
{  
System.out.println("B");  
}     
}

Output

Hybrid Inheritance in Java

Related Topics

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

What’s New in Java 15

Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in...

3 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Java Math signum() Method

The signum() method of Java Math class returns the signum function of the value. Syntax: public static double signum(double d)public static float signum (float d) Parameters: The parameter ‘d’ represents the floating-point value whose...

2 minutes read.

Java Math subtractExact() Method

The subtractExact() method of Java Math class returns mathematical difference of the specified two arguments, throwing an exception if the result overflows int or long. Syntax: public static int subtractExact (int x,...

1 minute read.

Java Math tan() Method

The tan() method of Java Math class returns the trigonometric tangent of the specified angle. Syntax: public static double tan(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The tan() method...

2 minutes read.

Java Instanceof Keyword

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). The type of comparison in java differentiates the...

4 minutes read.

How to remove special characters from String in Java

Strings in Java are Objects that are supported inside by a burn exhibit. Since exhibits are immutable (cannot develop), Strings are changeless too. A completely new String is made whenever...

5 minutes read.

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Java String equals() method

equals() method compares two string based on the their content. Syntax public boolean equals(Objects anObject) parameter anObject: Object to be compared with the current String. Returns It returns true when the current String is equivalent to...

1 minute read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

How to Create a Package in Java?

For the most part, how to create a package in Java generally, a package is defined as a collection of relevant or irrelevant items together in a very major way....

7 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.