22
DecHybrid Inheritance in Java
Hybrid Inheritance in Java: An Overview
Inheritance is one of the most powerful Object-Oriented Programming concepts in Java till now. In which a class can inherit attributes and behaviors from superclasses. This allows systematic designing and structuring of classes, enabling access to properties of different methods or classes. But In this Java Tutorial, we will explore more about Hybrid Inheritance , which will include What is Hybrid Inheritance in Java? and Why Use Hybrid Inheritance? We will also explore Hybrid inheritance in Java with examples. So, Let's first discuss a little bit about "What is Hybrid Inheritance ?".
Master full stack development with our Java Full Stack Developer Certification Course—learn from industry experts!
What is Hybrid Inheritance in Java?
- Hybrid Inheritance in Java is a combination of all inheritances.
- Single and hierarchical inheritances or multiple inheritance.
- For example, if we have a class Son and a class Daughter that extends the class Mother, and then there is another class Grandmother that extends the class, Mother, then this type of Inheritance is known as Hybrid Inheritance.
- Why? We observe two kinds of inheritance here- Hierarchical and Single Inheritance.
- In short, A hybrid inheritance combines more than two inheritance types, such as multiple and single.
Example of Hybrid Inheritance
Let's see how hybrid inheritance works in Java. Here we will take a real-world example and see the actual implementation of hybrid inheritance.
Using Single and Multiple Inheritance in Hybrid inheritance
Example:
class Parents
{
public void displayParents()
{
System.out.println("Two Parents");
}
}
interface Mother
{
public void show();
}
interface Father
{
public void show();
}
public class Child extends Parents implements Mother, Father
{
public void show()
{
System.out.println("Mother and Father are parents ");
}
public void displayChild()
{
System.out.println("Mother and Father have one child");
}
public static void main(String args[])
{
Child obj = new Child();
System.out.println("Implementation of Hybrid Inheritance in Java");
obj.show();
obj.displayChild();
}
}
Implementation of Hybrid Inheritance in Java
Mother and Father are parents
Mother and Father have one child
Fast-track your coding career with ScholarHat’s Java Full-Stack Developer course. | |
Training Name | Price |
Full-Stack Java Developer Course (Learn Today, Earn ₹3 - ₹9 LPA* Tomorrow) | Book a FREE Live Demo! |
Using Single and Hierarchical Inheritance in hybrid inheritance
In the following figure, the GrandMother is a superclass. The Mother class inherits the properties of the GrandMother class. Since Mother and GrandMother represent a single inheritance. Further, the Mother class is inherited by the Son and Daughter class. Thus, the Mother becomes the parent class for Son and Daughter. These classes represent the hierarchical inheritance. Combinedly, it denotes the hybrid inheritance.
Let's Elaborate this in Java Compiler:
class GrandMother
{
public void showG()
{
System.out.println("She is grandmother.");
}
}
class Mother extends GrandMother
{
public void showM()
{
System.out.println("She is mother.");
}
}
class Daughter extends Mother
{
public void showD()
{
System.out.println("She is daughter.");
}
}
public class Son extends Mother
{
public void showS()
{
System.out.println("He is Son.");
}
public static void main(String args[])
{
Daughter obj = new Daughter();
obj.showD();
obj.showM();
obj.showG();
Son obj2 = new Son();
obj2.showS();
obj2.showM();
obj2.showG();
}
}
Output:
She is daughter.
She is mother.
She is grandmother.
He is Son.
She is mother.
She is grandmother.
Benefits of Hybrid Inheritance in Java
- The use of hybrid inheritance plays an important role and Code Reusability is one of Use of it.
- It provides a more flexible way to design and structure classes in Java programs.
- We can create a hierarchy of classes while also incorporating functionality from multiple interfaces and this allows for adaptable and extensible design.
- It also allows polymorphism to us. In this Objects of child classes can be treated as instances of their parent class or interface, allowing flexible code.
Which Inheritance Is Not Supported in Java?
- The Multiple inheritances using classes are not supported.
- Java only allows for single inheritance, where a class can inherit from only one superclass to avoid the more complexities that arise from multiple inheritances.
- There are very few situations where multiple inheritances are truly needed.
- So it is recommended to avoid keeping the codebase simple and manageable.
- One of the problems with multiple inheritances is the Diamond problem.
Disadvantages of Hybrid Inheritance.
- It can lead to complex design hierarchies sometimes, It makes the code harder to understand and maintain by beginner users.
- As the number of classes and interfaces involved increases, the readability of the code may challenge for developers.
- In hybrid inheritance, there can be happen diamond problem because multiple inheritance can be included in it.
- Hybrid inheritance can be more time-consuming compared to simpler inheritance models.