Table of contents
1.
Introduction
2.
What is Hybrid Inheritance in Java?
3.
Why Do We Need Java Inheritance?
4.
Important Terminologies Used in Java Inheritance
5.
How Hybrid Inheritance Works in Java?
6.
Example of Hybrid Inheritance
6.1.
Code in Java
6.2.
Java
7.
Using Single and Multiple Inheritance
7.1.
Java
8.
Using Multilevel and Hierarchical Inheritance
8.1.
Java
9.
Using Hierarchical and Single Inheritance
9.1.
Java
10.
Using Multiple and Multilevel Inheritance
11.
Frequently Asked Questions
11.1.
Why is there no hybrid inheritance in java?
11.2.
What are the advantages of hybrid inheritance in Java?
11.3.
What are the two types of inheritance in Java?
12.
Conclusion
Last Updated: Aug 7, 2024
Easy

Hybrid Inheritance in Java

Introduction

Java is an object-oriented programming language. Everything in Java is an object, all the programs, codes, and data reside within classes and objects.  Inheritance, Polymorphism, Encapsulation, Abstraction, classes, and objects are few important concepts of Object-Oriented Programming.  OOPs is a really important topic from an interview perspective.

hybrid inheritance in java

As in real life, a child inherits some features from its parents; Similarly, in Object Oriented Programming language, a class may inherit from its parent class. In Java, every class has a superclass/parent class. The Object class is the root of the entire class hierarchy in Java.

Java supports the following types of inheritance:

  1. Single Inheritance
  2. Multi-level Inheritance
  3. Hierarchical Inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance(not supported by using classes, but its possible using interfaces)
     

This blog will discuss hybrid inheritance in Java in detail, along with codes and examples.

What is Hybrid Inheritance in Java?

Hybrid inheritance in Java occurs when a combination of two or more types of inheritance is within a single class hierarchy. Java supports multiple inheritance using interfaces and single inheritance through classes. When a class inherits properties and behaviors from another class and one or more interfaces, it shows hybrid inheritance.

Why Do We Need Java Inheritance?

In Java, inheritance is a fundamental concept that allows classes to inherit attributes and methods from other classes. Here's why inheritance is essential:

  • Code Reusability: Inheritance promotes code reusability by allowing subclasses to inherit features from superclasses. This eliminates the need to duplicate code, leading to cleaner and more maintainable codebases.
  • Hierarchy and Organization: Inheritance facilitates the creation of class hierarchies, where subclasses specialize or extend the functionality of their parent classes. This hierarchical structure provides a clear organization of related classes and promotes better understanding of the code.
  • Polymorphism: Inheritance enables polymorphism, allowing objects of subclasses to be treated as objects of their superclass. This enhances flexibility and enables the use of subclass objects interchangeably with superclass objects.
  • Encapsulation: Inheritance supports encapsulation by allowing access control modifiers to restrict access to superclass members. Subclasses can inherit and override superclass methods while adhering to encapsulation principles.
  • Code Extensibility: Inheritance allows for the extension of existing classes to accommodate new functionalities or requirements. Subclasses can add new methods or attributes without modifying the original superclass, promoting scalability and adaptability of the codebase.

Important Terminologies Used in Java Inheritance

In Java inheritance, several key terminologies are commonly used to describe the relationship between classes:

  • Superclass: A superclass, also known as a parent class or base class, is a class from which other classes, called subclasses, inherit attributes and methods.
  • Subclass: A subclass, also known as a derived class or child class, is a class that inherits attributes and methods from its superclass.
  • Inheritance: Inheritance is the mechanism by which a subclass inherits attributes and methods from its superclass.
  • extends Keyword: In Java, the extends keyword is used to establish an inheritance relationship between a subclass and its superclass. It is used in the class declaration of the subclass to specify its superclass.
  • Overriding: Overriding is the process of providing a new implementation for a method inherited from a superclass in a subclass. It allows subclasses to customize the behavior of inherited methods.
  • super Keyword: The super keyword is used to refer to the superclass of a subclass. It can be used to invoke superclass constructors or methods from within a subclass.
  • Is-A Relationship: Inheritance establishes an "is-a" relationship between a subclass and its superclass, indicating that the subclass is a specialized version of the superclass. For example, a Dog class "is-a" type of Animal class.

How Hybrid Inheritance Works in Java?

The fundamental goal of hybrid inheritance is to enhance code reuse by making it simpler for programmers to use the properties and methods that already exist in other classes. 

In Java, hybrid inheritance is essentially the fusion of multiple inheritance types. One or more combinations of "Single and Multiple Inheritance in Java", "Hierarchical and Single InheritanceMultilevel", and "Hierarchical Inheritance," or any other combination depending on the requirements of your project.

Now, you might have a doubt about how to implement hybrid inheritance in Java. Let us discuss this with the help of an example. 

Example of Hybrid Inheritance

Here is an example to understand hybrid inheritance in Java. Suppose we have a ninjas hierarchy that consists of a base class called Ninjas, which represents common properties and behaviors of ninjas. Then, we have two subclasses, Ninja1 and Ninja2, which inherit from the Ninjas class. We also have two interfaces, CompetitiveCoder and NormalCoder, which represent specific types of ninjas based on their skills. The CompetitiveCoder interface defines methods related to ninjas with competitive coding. The NormalCoder interface defines methods related to ninjas with normal coding. Let us try to implement this with the help of hybrid inheritance:

Code in Java

  • Java

Java

// Creating interfaces
interface CompetitiveCoder {
void competitive();
}
interface NormalCoder {
void normal();
}

// Creating a Parent Class with a method greeting()
class Ninjas {
void greeting() {
System.out.println("Hello Ninja!!");
}
}

// First child class
class Ninja1 extends Ninjas implements NormalCoder {
public void normal() {
System.out.println("Hey, Ninja1 you can start with the normal coding.");
}
}

// Second child class
class Ninja2 extends Ninjas implements NormalCoder, CompetitiveCoder {
public void normal() {
System.out.println("Hey, Ninja2 first you should start with normal coding.");
}
public void competitive() {
System.out.println("Hey, Ninja2 now you can start with competitive programming.");
}
}
public class Main {
public static void main(String[] args) {
// Calling methods for Ninja1
Ninja1 ninja1=new Ninja1();
ninja1.greeting();
ninja1.normal();

System.out.println();

// Calling methods for Ninja2
Ninja2 ninja2=new Ninja2();
ninja2.greeting();
ninja2.normal();
ninja2.competitive();
}
}
You can also try this code with Online Java Compiler
Run Code

 

This code will give the following output:

Hello Ninja!!
Hey, Ninja1 you can start with the normal coding.
Hello Ninja!!
Hey, Ninja2 first you should start with normal coding.
Hey, Ninja2 now you can start with competitive programming.


Explanation

In this example, the Ninja1 class inherits from the Ninjas class using single inheritance. It also implements the NormalCoder interface. The Ninja2 class inherits from the Ninjas class and implements both the NormalCoder and CompetitiveCoder interfaces. This showcases multiple inheritance through interfaces. Then we have created instances of the Ninja1 and Ninja2 classes for invoking their methods.

Using Single and Multiple Inheritance

Consider the following example wherein a combination of single and multiple inheritances is used to achieve hybrid inheritance in Java:

There is a HumanBody class that has some functionality like eating(), walking(), dancing(), etc., and some fields like bloodPressure, normalOxygenLevel, bonesCount, etc. There are two child classes of the HumanBody class, the Female and Male class. The Male class and the Female class will inherit all the functions and the fields of the HumanBody class. This is a simple example of Single Inheritance in Java. 

Using Single Inheritance

Now consider that the Male and Female classes have a child with the class name as Child. This is a simple example of multiple inheritances in Java. The relationship between the Male, Female, and Child classes is visualized below:

Using Multiple Inheritance

 

The combined relationship between the HumanBody, Male, Female, and Child classes is visualized below. It is an example of Hybrid Inheritance using the combination of Single and Multiple Inheritances.

Using Single and Multiple Inheritance

 

However, wait, we know that multiple inheritances are not possible in Java. So this analogy has to be incorrect. 

Well, the only incorrect thing is that, instead of classes, interfaces will be used to achieve multiple inheritances.

Syntax

public class A
{
    // Methods and Fields of class A
}
public interface interfaceB 
{
  // Methods and Fields of InterfaceB
}
public interface interfaceC 
{
  // Methods and Fields of InterfaceC
}
public class D extends A implements InterfaceB,InterfaceC
{
    // Implementation of the method defined in Interfaces, InterfaceB and InterfaceC

    // Methods of class D   
}

 

The below program demonstrates hybrid inheritance using a combination of single inheritance and multiple inheritances using Interfaces.

  • Java

Java

class HumanBody
{
   public void displayHuman()
   {
     System.out.println("Method defined inside HumanBody class");
   }
}
interface Male
{
 public void show();
}
interface Female
{
 public void show();
}
 
// Single Inheritance is the relationship between Child // class and HumanBody class.
// Implementing Male and Female interface is Multiple // Inheritance
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();
 }
}
You can also try this code with Online Java Compiler
Run Code

 

The output of the above program is: You can Execute this java code here

Implementation of Hybrid Inheritance in Java
Implementation of show() method defined in interfaces Male and Female
Method defined inside Child class

Using Multilevel and Hierarchical Inheritance

Hybrid Inheritance can also be achieved using a combination of Multilevel and Hierarchical inheritance. A real-world example will be, Son class inherits the Father class, Father class inherits the GrandFather class. This relation is of Multilevel inheritance. Likewise, A Daughter Class inherits the Father class, which in turn inherits the GrandFather class.

On the other hand, Son and Daughter both inherit Father class, and this relation is of Hierarchical Inheritance.

A combination of both the inheritance relation will make Hybrid inheritance.

Using Multilevel and Hierarchical Inheritance

 

The below program demonstrates Hybrid inheritance using a combination of Multilevel and Hierarchical inheritance.

Syntax

  • Java

Java

class GrandFather
{
 public void printGrandFather()
 {
   System.out.println("GrandFather's class");
 }
}
 
class Father extends GrandFather
{
 public void printFather()
 {
   System.out.println("Father class has inherited GrandFather class");
 }
}
 
class Son extends Father
{
 public Son()
 {
   System.out.println("Inside the Son Class");
 }
 
 public void printSon()
 {
   System.out.println("Son class has inherited Father class");
 }
}
 
class Daughter extends Father
{
 public Daughter()
 {
   System.out.println("Inside the Daughter Class");
 }
 
 public void printDaughter()
 {
   System.out.println("Son class has inherited Father class");
 }
}
 
public class HybridInheritance
{
 public static void main(String[]args)
 {
   Son obj = new Son();
   obj.printSon();  // Accessing Son class method
   obj.printFather();  // Accessing Father class method
   obj.printGrandFather();  // Accessing GrandFather class method
 
   Daughter obj2 = new Daughter();
   obj2.printDaughter();  // Accessing Daughter class method
   obj2.printFather();    // Accessing Father class method
   obj2.printGrandFather();   // Accessing GrandFather class method 
 
 }
}
You can also try this code with Online Java Compiler
Run Code

 

The output of the above program is:

Inside the Son Class
Son class has inherited Father class
Father class has inherited GrandFather class
GrandFather's class
Inside the Daughter Class
Son class has inherited Father class
Father class has inherited GrandFather class
GrandFather's class

 

Using Hierarchical and Single Inheritance

Here class Son and class Daughter extends class Father which represents the hierarchical inheritance. On the other hand, class Student extends the class Daughter that, represents the single inheritance.

Using Hierarchical and Single Inheritance

We will combine these hierarchical and single classes to create a hybrid class.

The below program demonstrates Hybrid inheritance using a combination of Hierarchical and Single inheritance.

Syntax

  • Java

Java

class Father {
public void disp() {
System.out.println("This is the father class");
}
}
class Son extends Father {
public void disp() {
System.out.println("The Son class extends the Father Class");
}
}
class Daughter extends Father {
public void disp() {
System.out.println("The Daughter class extends the Father Class");
}
}
public class Student extends Daughter {
public void disp() {
System.out.println("Method defined inside the Student class");
}
public static void main(String args[]) {
Student obj = new Student();
System.out.println("Example of Hybrid Inheritance using Hierarchical and Single Inheritance");
obj.disp();
}
}
You can also try this code with Online Java Compiler
Run Code

 

The output of the above program is:

Example of Hybrid Inheritance using Hierarchical and Single Inheritance
Method defined inside the Student class

 

Using Multiple and Multilevel Inheritance

Here is an example of hybrid inheritance using multiple and multilevel inheritances.

Using Multiple and Multilevel Inheritance

Java classes do not enable multiple inheritances. Managing the complexity produced by multiple inheritances is extremely challenging, and multiple inheritances are rarely necessary; therefore, it is preferable to do without them to make things clear-cut and simple.

Implementing multiple interfaces in a class is the only way to implement multiple inheritances.

Frequently Asked Questions

Why is there no hybrid inheritance in java?

Java supports hybrid Inheritance. However, Java does not support Multiple Inheritance with classes, so in order to achieve hybrid inheritance, multiple inheritances with classes should not be used. Multiple Inheritance with Interfaces is possible.

What are the advantages of hybrid inheritance in Java?

Hybrid inheritance in Java allows for a combination of multiple inheritance and hierarchical inheritance, which provides flexibility and reduces code duplication. It also enables the creation of complex class hierarchies with multiple levels of abstraction.

What are the two types of inheritance in Java?

There are two types of inheritance in Java, i.e., single and multiple inheritance. Single inheritance refers to the situation when a class extends only one superclass. Java doesn't support multiple inheritance of classes, meaning a class cannot extend more than one class. But we can achieve multiple inheritance using interfaces in Java.

Conclusion

This blog discussed Hybrid Inheritance in Java in detail. With this done, you may now switch to learn more about Object-Oriented Programming concepts. 

Access modifiers in java

Questions related to Inheritance are frequently asked in technical rounds of big companies.

Do not stop here, feel free to explore Coding Ninjas Studio for practicing various coding problems.

Remember, Practice makes perfect !!

Live masterclass