• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Types of Inheritance in Java – Single,Multiple,Multilevel,Hierarchical & Hybrid

August 17, 2015 by javainterviewpoint 24 Comments

Below are the different types of inheritance which is supported by Java.

  • Single Inheritance
  • Multiple Inheritance (Through Interface)
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance (Through Interface)

Lets see about each one of them one by one.

1. Single Inheritance in Java

Single Inheritance is the simple inheritance of all, When a class extends another class(Only one class) then we  call it as Single inheritance. The below diagram represents the single inheritance in java where Class B extends only one class Class A. Here Class B will be the Sub class and Class A will be one and only Super class.Single_Inheritance_in_Java
Single Inheritance Example

public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
    public static void main(String args[])
    {
        //Assigning ClassB object to ClassB reference
        ClassB b = new ClassB();
        //call dispA() method of ClassA
        b.dispA();
        //call dispB() method of ClassB
        b.dispB();
    }
}

Output :

disp() method of ClassA
disp() method of ClassB

2. Multiple Inheritance in Java

Multiple Inheritance is nothing but one class extending more than one class. Multiple Inheritance is basically not supported by many Object Oriented Programming languages such as Java, Small Talk, C# etc.. (C++ Supports Multiple Inheritance). As the Child class has to manage the dependency of more than one Parent class. But you can achieve multiple inheritance in Java using Interfaces.
Multiple_Inheritance_in_Java

3. Multilevel Inheritance in Java

In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class. As seen in the below diagram. ClassB inherits the property of ClassA and again ClassB act as a parent for ClassC. In Short ClassA parent for ClassB and ClassB parent for ClassC.

Multilevel_Inheritance_in_Java

MultiLevel Inheritance Example

public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassB
{
    public void dispC()
    {
        System.out.println("disp() method of ClassC");
    }
    public static void main(String args[])
    {
        //Assigning ClassC object to ClassC reference
        ClassC c = new ClassC();
        //call dispA() method of ClassA
        c.dispA();
        //call dispB() method of ClassB
        c.dispB();
        //call dispC() method of ClassC
        c.dispC();
    }
}

Output :

disp() method of ClassA
disp() method of ClassB
disp() method of ClassC

4. Hierarchical Inheritance in Java

In Hierarchical inheritance one parent class will be inherited by many sub classes. As per the below example ClassA will be inherited by ClassB, ClassC and ClassD. ClassA will be acting as a parent class for ClassB, ClassC and ClassD.

Hierarchical_Inheritance_in_Java

Hierarchical Inheritance Example

public class ClassA 
{
    public void dispA()
    {
        System.out.println("disp() method of ClassA");
    }
}
public class ClassB extends ClassA 
{
    public void dispB()
    {
        System.out.println("disp() method of ClassB");
    }
}
public class ClassC extends ClassA
{
    public void dispC()
    {
        System.out.println("disp() method of ClassC");
    }
}
public class ClassD extends ClassA
{
    public void dispD()
    {
        System.out.println("disp() method of ClassD");
    }
}
public class HierarchicalInheritanceTest 
{
    public static void main(String args[])
    {
        //Assigning ClassB object to ClassB reference
        ClassB b = new ClassB();
        //call dispB() method of ClassB
        b.dispB();
        //call dispA() method of ClassA
        b.dispA();
        
        
        //Assigning ClassC object to ClassC reference
        ClassC c = new ClassC();
        //call dispC() method of ClassC
        c.dispC();
        //call dispA() method of ClassA
        c.dispA();
        
        //Assigning ClassD object to ClassD reference
        ClassD d = new ClassD();
        //call dispD() method of ClassD
        d.dispD();
        //call dispA() method of ClassA
        d.dispA();
    }
}

Output :

disp() method of ClassB
disp() method of ClassA
disp() method of ClassC
disp() method of ClassA
disp() method of ClassD
disp() method of ClassA

5. Hybrid Inheritance in Java

Hybrid Inheritance is the combination of both Single and Multiple Inheritance. Again Hybrid inheritance is also not directly supported in Java only through interface we can achieve this. Flow diagram of the Hybrid inheritance will look like below. As you can ClassA will be acting as the Parent class for ClassB & ClassC and ClassB & ClassC will be acting as Parent for ClassD.

Hybrid_Inheritance_in_Java

Hope you have got a better understanding towards the different types of inheritance in Java. Happy Learning 🙂

Other interesting articles which you may like …

  • JVM Architecture – Understanding JVM Internals
  • Object and Object Class in Java
  • Difference between JDK, JRE and JVM
  • Components of Java Development Kit (JDK)
  • What is a Class in Java with Example
  • How to open .class file in Java
  • How to Set Classpath for Java in Windows
  • ClassNotFoundException Vs NoClassDefFoundError
  • How HashMap works in Java
  • How to make a class Immutable in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of polymorphism in Java

Filed Under: Core Java, Java, OOPs Tagged With: Hierarchical, Hierarchical Inheritance, Hybrid, Hybrid Inheritance, Java, Multilevel, Multilevel Inheritance, Multiple, Multiple Inheritance, Single, Single Inheritance, Types of Inheritance

Comments

  1. Sairam says

    October 14, 2016 at 12:39 pm

    nice

    Reply
  2. kuber says

    November 2, 2016 at 11:22 pm

    usefull for examination

    Reply
  3. Shivani says

    November 28, 2016 at 7:33 pm

    Thank you……………nicely briefly explained

    Reply
  4. Asim says

    December 8, 2016 at 1:27 am

    Nice

    Reply
  5. siva says

    December 22, 2016 at 1:40 pm

    Thnk u 4 ur information…….. It’s useful 4 me

    Reply
  6. MUKESH says

    December 23, 2016 at 10:38 am

    AWESOME

    Reply
  7. Sony kashyap says

    December 26, 2016 at 2:58 pm

    Good material for easy self learning

    Reply
  8. muhammad daniyal says

    January 31, 2017 at 9:07 am

    thanks!!

    Reply
  9. kirankumar says

    February 9, 2017 at 12:06 pm

    good

    Reply
  10. Sirichandana says

    February 9, 2017 at 2:00 pm

    Nice…good explanation.tnqq
    It is very useful

    Reply
  11. jiya says

    February 26, 2017 at 9:29 am

    thank you

    Reply
  12. munisha says

    April 2, 2017 at 7:19 pm

    awesome explanation

    Reply
  13. Marshal frank says

    June 12, 2017 at 2:03 pm

    Very accurate results and vital one.

    Reply
  14. Neha says

    September 20, 2017 at 9:14 pm

    Easy to understand and it helped me a lot…. thankyou

    Reply
  15. Sher Says says

    September 25, 2017 at 8:48 pm

    awesome explanation
    easy to understand

    Reply
  16. Marin says

    September 30, 2017 at 12:16 am

    It was really useful. Thnk u

    Reply
  17. Satpal sharma says

    December 3, 2017 at 8:48 pm

    Good explain

    Reply
  18. Sharmin Nahar says

    January 21, 2018 at 5:01 pm

    Thanks… ????

    Reply
  19. Anshul Chawla says

    May 18, 2018 at 7:06 am

    Appropriate material for last time revision during exams.

    Reply
  20. Ìñßæñê says

    June 30, 2018 at 5:20 pm

    Well defined explanation.????

    Reply
  21. Sai Kumar Reddy says

    August 7, 2018 at 2:59 pm

    That was amazing explanation thq so much

    Reply
  22. Hitendra says

    August 10, 2018 at 8:56 am

    Awesome explanation ….

    Reply
  23. Shanu says

    August 17, 2018 at 10:15 pm

    Very useful. Thank u so much

    Reply
  24. Fakhre Alam says

    February 29, 2020 at 2:30 pm

    Nicely Explained .
    Thank you so much .

    Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·