Types of Inheritance in Java
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.
Best-suited Java courses for you
Learn Java with these high-rated online courses
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:
- Single-level inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
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:
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:
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().
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).
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.
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:
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.
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