×

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 Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Perfect Number Program in Java

Perfect Number Program in Java A perfect number is a number whose sum of all the factors, excluding the number itself, is equal to the number. For example, 28 is a...

4 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Stock Span Problem Using Stack in Java

The stock span problem is an issue in finance where we must determine a stock’s price span over all N days given a set of N daily price quotes. The...

6 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Permutation Coefficient in Java

In this tutorial, we will get familiar with the permutation coefficient in Java.  We will understand it through examples and see different approaches to solving the problem. A permutation is a...

5 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Comparetoignorecase in Java

In Java, the function compareToIgnoreCase ( ) is part of the String class, which is part of the java.lang package. It is used to compare any two strings while disregarding...

4 minutes read.

How to use scanner in Java

Scanner class in Java is the part of java.util package. Java programming language has various ways to read input from the user, Scanner class is one of the classes to...

5 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.