Types of Inheritance in Java

Types of Inheritance in Java

8 mins readComment
Esha
Esha Gupta
Associate Senior Executive
Updated on Oct 13, 2024 21:26 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.

Recommended online courses

Best-suited Java courses for you

Learn Java with these high-rated online courses

– / –
6 weeks
β‚Ή15 K
3 months
β‚Ή2.05 K
3 months
– / –
170 hours
β‚Ή5.4 K
1 month
– / –
6 months
– / –
2 months
β‚Ή17 K
3 months
β‚Ή5.5 K
1 month

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

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

Difference Between Multilevel and Multiple Inheritance

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

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?

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

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

Control Statements in Java - About and Types

Understanding Java Main Method

Understanding Variables in Java

Java Comments | About, Types and Examples

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