Types of Inheritance in Java

Types of Inheritance in Java

8 mins readComment
clickHere
Esha
Esha Gupta
Associate Senior Executive
Updated on Apr 17, 2024 14:13 IST

Have you ever wondered about the ways in which Java allows for code reusability and hierarchical organization through inheritance? Java supports several types of inheritance: single inheritance through class extension, multilevel inheritance to create a chain of class relationships, hierarchical inheritance for multiple classes to share a single superclass, and multiple inheritance through interfaces for a class to adopt methods from multiple sources. Hybrid inheritance combines these approaches, offering flexibility in complex software design. Let's understand more!

Inheritance in Java is a fundamental Object-Oriented Programming (OOP) concept that allows one class to inherit fields and methods from another class. Inheritance enables code reuse and establishes a parent-child relationship between classes, where the parent class is referred to as the superclass and the child class as the subclass.

For example, Different book genres like "Fiction" inherit basic characteristics of a general "Book" class but also have additional attributes unique to them.

The 5 types of inheritance related to Java are given below:

NOTE: Java does not support Multiple Inheritance and Hybrid Inheritance directly through classes due to its language design, it facilitates aspects of these inheritance types using interfaces and other mechanisms. We will learn about it in detail in later part of the blog. Stay tuned!

Let's understand each one of these one by one in detail!

1. Single-level inheritance

Single-level inheritance in Java is a fundamental concept where a subclass inherits from only one superclass. This means that the subclass can acquire the properties and methods of the superclass, enabling code reuse and the extension of existing functionality.

A graphical illustration of single-level inheritance is given below:

 

Let's Understand it with a Real-Life Analogy.

Think of single-level inheritance, like passing down a family recipe from one generation to the next. Imagine you have a cherished family cookbook that has been passed down from your grandparents (the superclass) to your parents (the subclass). This cookbook contains a wealth of recipes (methods) and cooking tips (properties). Your parents inherit this cookbook along with all its contents. They can use the recipes as they are, add new recipes of their own, or even modify some of the existing ones to suit their taste better. However, the core of the cookbook, the original recipes from your grandparents, remains accessible and preserved.

In this analogy:

  • The grandparents' cookbook represents the superclass in Java, holding valuable information (methods and properties).
  • The act of your parents receiving this cookbook symbolizes single-level inheritance, where the subclass (your parents) inherits from the superclass (the grandparents).
  • The ability of your parents to use, modify, or add to the cookbook mirrors how a subclass in Java can utilize inherited methods, override them, or introduce new methods and properties.

All About Single Inheritance in Java
All About Single Inheritance in Java
Have you ever wondered about the simplicity and efficiency of single inheritance in Java? It allows a class to inherit properties and methods from one parent class, simplifying code development...read more

2. Multi-level Inheritance

Multilevel inheritance in Java is a feature that allows a class to inherit properties and behaviours from another class, which in turn inherits from another class, forming a "chain" of inheritance. This mechanism enables a class to inherit methods and fields from multiple ancestors but in a direct line, where each class in the chain inherits from one class directly above it.

A graphical illustration of multi-level inheritance is given below:

Let's Understand it with a Real-Life Analogy

Imagine a hierarchy of transportation modes, starting from a basic form of transportation and becoming more specialized as we move down the hierarchy.

  • Vehicle (Level 1): This is the most general class in our system. It includes properties and methods common to all types of vehicles, such as the ability to start or stop and attributes like weight and colour.
  • Land Vehicle (Level 2): Inherits from Vehicle. This class adds more specific features relevant to vehicles that operate on land, such as the number of wheels and type of engine. It still maintains the general attributes and behaviours of a Vehicle.
  • Car (Level 3): Inherits from Land Vehicle. This class is more specialized and might introduce attributes and methods specific to cars, such as seating capacity and car model. It inherits properties and methods from both Vehicle and Land Vehicle, like weight, color, number of wheels, and engine type, but adds its own specific features on top.
  • Electric Car (Level 4): Inherits from Car. This is an even more specialized class that adds features specific to electric cars, such as battery capacity and charging time. It inherits all attributes and methods from Vehicle, Land Vehicle, and Car but tailors the functionality to fit electric cars specifically.

Mastering Multilevel Inheritance in Java
Mastering Multilevel Inheritance in Java
Do you know multi-level inheritance in Java simplifies building complex systems by letting classes inherit features and behaviours from their ancestors? This method enhances code efficiency and maintainability, enabling developers...read more

Difference Between Multilevel and Multiple Inheritance
Difference Between Multilevel and Multiple Inheritance
Have you ever wondered about the difference between multilevel and multiple inheritance in OOP? Multilevel follows a parent-child chain for extending classes, while multiple allows a class to inherit features...read more

3. Hierarchical Inheritance

Hierarchical inheritance in Java occurs when one base class (superclass) is inherited by multiple subclasses. In this type of inheritance, all the features that are common in child classes are included in the base class. This way, the code becomes more manageable and reusable.

A graphical illustration of hierarchical inheritance is given below:

 

Let's Understand it with a Real-Life Analogy.

Imagine a school system to explain hierarchical inheritance in Java, where one base class is inherited by multiple subclasses.

  • Base Class: SchoolMember - This class acts as the superclass for all types of people involved in a school. It could include properties like name, age, and address and methods like showDetails() to display these properties.
  • Subclasses: Teacher, Student, and Staff - These classes inherit from SchoolMember. Each subclass represents a different category of school member with its unique attributes and behaviors.
  • Teacher might introduce additional properties like subjectSpecialization and methods like teach().
  • Student could add properties such as gradeLevel and methods like study().
  • Staff might have properties like department and methods like work().

Exploring Hierarchical Inheritance in Java
Exploring Hierarchical Inheritance in Java
Hierarchical inheritance in Java allows exactly this, letting multiple subclasses inherit from a single superclass, enabling both shared functionalities and individual characteristics to coexist harmoniously in a structured and efficient...read more

4. Multiple Inheritance

Multiple inheritance refers to a feature in object-oriented programming where a class can inherit properties and methods from more than one parent class. This concept allows a subclass to inherit behaviours and attributes from multiple base (or super) classes, creating a rich, multifaceted hierarchy. 

However, it's important to note that Java does not support multiple inheritance for classes directly due to the complexity and potential for ambiguity it introduces (such as the "Diamond Problem", where a class inherits from two classes that have a common ancestor, leading to ambiguity in which ancestor's method or property to use).

Why Multiple Inheritance is Not Supported in Java?
Why Multiple Inheritance is Not Supported in Java?
Have you ever wondered why Java doesn't support multiple inheritance directly? The primary reason is to avoid complexity and ambiguity in the inheritance hierarchy, especially problems like the diamond issue....read more

A graphical illustration of multiple inheritance is given below:

Instead of multiple inheritance for classes, Java provides a mechanism to achieve polymorphism and code reuse through interfaces. An interface in Java is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructor methods. A class in Java can implement multiple interfaces, allowing it to inherit abstract methods from multiple sources.

Learning Multiple Inheritance in Java
Learning Multiple Inheritance in Java
Have you ever wondered how Java addresses the concept of multiple inheritance and the Diamond Problem? Java avoids these complexities by enabling classes to implement multiple interfaces, allowing for multiple...read more

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance, such as hierarchical, multiple, multilevel, or single inheritance. It integrates various inheritance mechanisms to achieve a specific design or functionality. However, it's important to note that due to Java's restriction on extending multiple classes directly (to avoid complexity and ambiguity issues like the Diamond Problem), hybrid inheritance in Java is primarily achieved through a mix of class inheritance (using the extends keyword) and interface implementation (using the implements keyword).

In Java, hybrid inheritance can involve:

  • Single and Multilevel Inheritance through classes: A class inherits from one superclass, and then another class inherits from that subclass, forming a linear inheritance chain.
  • Hierarchical Inheritance through classes: Multiple classes inherit from a single parent class.
  • Multiple Inheritance through Interfaces: A class implements multiple interfaces, inheriting the abstract methods from these interfaces.
  • Combining Class Inheritance and Interface Implementation: A class extends another class and also implements one or more interfaces.

A graphical illustration of hybrid inheritance is given below:

Understanding Hybrid Inheritance in Java
Understanding Hybrid Inheritance in Java
Have you ever wondered about what is hybrid inheritance in Java? It's a technique combining two or more inheritance, allowing a class to inherit properties and behaviours from various sources,...read more

Control Statements in Java - About and Types
Control Statements in Java - About and Types
Have you ever wondered how Java manages the flow of its programs so efficiently? The three types of control statements, namely decision-making statements, looping statements, and jump statements, allow Java...read more

Understanding Java Main Method
Understanding Java Main Method
Have you ever wondered how a Java application begins its execution? The key lies in the Java main method, public static void main(String[] args), which serves as the gateway for...read more

Understanding Variables in Java
Understanding Variables in Java
Have you ever wondered how data is stored and manipulated in Java programs? Variables in Java are the answer, acting as containers for data values. Each variable is defined with...read more

Java Comments | About, Types and Examples
Java Comments | About, Types and Examples
Do you know what makes the code more readable? Comments, as they provide valuable context and explanations about the code, making it easier for both the original developers and others...read more

Thus, Java's approach to inheritance emphasizes clarity, simplicity, and the avoidance of ambiguity, particularly with its decision to disallow direct multiple-class inheritance. Instead, Java offers a flexible system through interfaces and abstract classes to achieve similar outcomes without the inherent problems of multiple inheritance seen in other languages.

Check out Java Courses Here!

FAQs

What is Inheritance in Java?

Inheritance in Java is a mechanism where one class is allowed to inherit the fields and methods of another class. The class that inherits is called the subclass or child class, and the class from which it inherits is called the superclass or parent class.

How Many Types of Inheritance are Supported in Java?

Java supports four types of inheritance: single, multilevel, hierarchical, and hybrid. However, Java does not support multiple inheritance with classes to avoid complexity and simplify design.

What is Single Inheritance?

Single inheritance is when a class inherits from only one superclass. This is the simplest form of inheritance, where the subclass gains access to the public and protected members of the superclass.

What is Multilevel Inheritance?

Multilevel inheritance refers to a scenario where a class is derived from a subclass, making it a chain of inheritance. For example, if class C inherits from class B, and class B inherits from class A, then this is a case of multilevel inheritance.

What is Hierarchical Inheritance?

Hierarchical inheritance occurs when multiple subclasses inherit from a single superclass. This allows different child classes to share the same superclass methods and fields, while also implementing their unique attributes and behaviors.

Why Doesn’t Java Support Multiple Inheritance?

Java does not support multiple inheritance with classes to prevent ambiguity and the "Diamond Problem," where a class inherits from two classes that have a method with the same signature. However, Java provides a workaround by allowing a class to implement multiple interfaces.

Can Interfaces Support Multiple Inheritance in Java?

Yes, in Java, interfaces can support multiple inheritance. A single class can implement multiple interfaces, allowing it to inherit abstract methods from multiple sources, thereby achieving multiple inheritance.

What is a Hybrid Inheritance?

Hybrid inheritance is a combination of two or more types of inheritance. Since Java does not support multiple inheritance with classes, hybrid inheritance is typically achieved through a mix of classes and interfaces.

What is the super Keyword in Java?

The super keyword in Java is used in subclass methods to refer to the superclass's methods and constructors. It is particularly useful for accessing and invoking the superclass's constructor and for accessing overridden methods.

How Does Java Achieve Code Reusability Through Inheritance?

Java achieves code reusability through inheritance by allowing subclasses to inherit accessible fields and methods from the superclass. This mechanism reduces redundancy and enhances modularity by allowing child classes to reuse and extend the functionalities of their parent classes without rewriting code.

About the Author
author-image
Esha Gupta
Associate Senior Executive

Hello, world! I'm Esha Gupta, your go-to Technical Content Developer focusing on Java, Data Structures and Algorithms, and Front End Development. Alongside these specialities, I have a zest for immersing myself in v... Read Full Bio