BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

hybrid inheritance in java with example program

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

A hybrid inheritance is a combination of more than one types of inheritance. For example when class A and B extends class C & another class D extends class A then this is a hybrid inheritance, because it is a combination of single and hierarchical inheritance. Let me show you this diagrammatically:

         C
         ↑
         |
  ---------------
  ↑             ↑
  |             |
  A             B
  ↑
  |
  D

If you want to learn the basics of inheritance, refer this guide: java inheritance

Hybrid Inheritance in Java

Hybrid inheritance

It seems that because of this diagram people are finding it difficult to understand this topic because this diagram shows combination of hierarchical and multiple inheritance and multiple inheritance is not supported in java.
The diagram is just for the representation, since multiple inheritance is not possible in java, It is not correct to show that as a part of hybrid inheritance. I will update the diagram whenever I get the time. You can refer the example that I have given at the beginning of post representing combination of single and hierarchical inheritance

Hybrid Inheritance Example

Lets write this in a program to understand how this works:

         C
         ↑
         |
  ---------------
  ↑             ↑
  |             |
  A             B
  ↑
  |
  D

Program: This example is just to demonstrate the hybrid inheritance in Java. Although this example is meaningless, you would be able to see that how we have implemented two types of inheritance(single and hierarchical) together to form hybrid inheritance.
Class A and B extends class C → Hierarchical inheritance
Class D extends class A → Single inheritance

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");
   }
	
}

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

	D obj = new D();
	obj.disp();
   }
}

Output:

D
❮ PreviousNext ❯

Top Related Articles:

  1. Multithreading in java with examples
  2. Java date difference
  3. Java – Static Class, Block, Methods and Variables
  4. Constructor Overloading in Java with examples
  5. How to write to file in Java using BufferedWriter

Tags: Java-OOPs

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Harsh says

    September 4, 2014 at 8:28 PM

    Is it possible that a class(C) extends a class (B) and implements an interface(A) as well?
    something like : class c extends B ,implements A{}

    Reply
    • Ram Narayan says

      March 4, 2016 at 6:46 AM

      Yes Possible …

      class XYZ extends ABC implements DEF
      {
      // code executes without error
      }

      Reply
    • Anuj says

      March 2, 2017 at 2:12 AM

      In fact here in implemention you can implement more than 1 interface , as in you can implement as many as you want ,but extends only one class .

      Reply
  2. Superman says

    October 8, 2014 at 5:41 AM

    Hello Harsh,
    Class can’t extend interface so it’s not possible.

    Have a good day.

    Reply
    • Gagandeep Singh says

      December 7, 2016 at 4:48 PM

      Where do u see a class extending any interface .. ?

      Reply
  3. techykudos says

    February 12, 2015 at 2:15 PM

    Why not allowed abstract classes instead of interfaces in same approach? In that approach also we can implement the abstract method to avoid compiler confusion.

    Reply
  4. kshitij says

    July 10, 2015 at 4:22 PM

    If we have to implement all the methods inherited then what is the use of inheriting them anyway? I don’t see code reusability

    Reply
    • Sanyam Trehan says

      July 5, 2017 at 6:18 PM

      These are just small examples to understand the concept of inheritance. When working on a live project like when implementing thread or runnable interfaces then code reusability is used.

      Reply
  5. Jeya Chitra says

    August 7, 2015 at 10:28 AM

    Is it possible to combine single and hierarchical inheritance to form hybrid inheritance…….. If possible please give me a solution.

    Reply
    • Gaurav Kumar Garg says

      August 16, 2017 at 9:58 AM

      Hello Jeya Chitra,
      It is possible, you can combine single inheritance and hierarchical inheritance.

      I am giving one example where test5, test6 class extending class test4, Then class test7 is extending test5.
      First we are applying hierarchical inheritance than we are implementing single inheritance.

      Please check below example and try to understand how it is possible.

      // Example of MultiLevel Inheritance
      class test4
      {
      public void check()
      {
      System.out.println(“Calling In Test2”);
      }
      }

      class test5 extends test4
      {
      public void check2()
      {
      System.out.println(“Calling In Test3”);
      }
      }

      class test6 extends test4
      {
      public void check()
      {
      System.out.println(“Calling In Test2”);
      }
      }

      class test7 extends test5
      {
      public void check()
      {
      System.out.println(“Calling In Test2”);
      }
      }

      class hybrid_inherit extends test3
      {
      /**
      Calling test1 & test2 class method from Multiple inherit class creating object.
      */
      public static void main(String…args){

      multi_lvl_inherit sh = new multi_lvl_inherit();
      sh.check2();
      sh.check();
      }
      }

      Reply
      • Chaitanya Singh says

        September 11, 2017 at 8:19 AM

        Nice Example Gaurav. Thanks for posting it here.

        Reply
  6. Ram Narayan says

    March 4, 2016 at 6:42 AM

    “class B and C both are extending class A and they both have overridden the methodA(), which they can do as they have extended the class A. But since both have different version of methodA(), compiler is confused which one to call when there has been a call made to methodA() in child class D (child of both B and C, it’s object is allowed to call their methods), this is a ambiguous situation and to avoid it, such kind of scenarios are not allowed in java.”

    sir, here i have a doubt … you said that we are overriding the same method in both child class of A(i.e. B and C).so it’s not allowing to extend both the classes in D. all good !!!

    what if we are not overriding any value but want to use some other method from B and C class(no overriding)..say

    class B
    {
    public void methodinB()
    {
    syso(“i am in B”);
    }
    }
    public class c
    {
    public void methodinC()
    {
    syso(“i am in C”)
    }
    }

    public class D extends B,C
    {
    // This is also not allowed here, but we are not overriding here.. Why is this not allowed ???
    }

    Reply
    • Chaitanya Singh says

      March 7, 2016 at 8:07 AM

      Dear Ram Narayan, The program you have mentioned would throw error because multiple inheritance is not allowed in java and I have already explained why its not allowed.

      Reply
  7. Sachin says

    July 23, 2017 at 3:04 AM

    Brother hybrid inheritance is a combination of two type inheritances it is not necessary to take a single and a multiple inheritance we can also take other things like single and Hierarchical inheritance etc. So Java does support Hybrid inheritance but there is a fact that there should not be a case where two classes are having the same child class.if I am wrong please let me know.

    Reply
    • Chaitanya Singh says

      September 11, 2017 at 8:23 AM

      Absolutely correct Sachin, I think you got confused because of the diagram I have shown here. The diagram is just for the representation, since multiple inheritance is not possible in java. It is not correct to show that as a part of hybrid inheritance. I will update the diagram whenever I get the time. I will also post this as a note in the above post so that others don’t get confused. Thanks for taking the time to post a comment here.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap