Java supports different types of inheritance that allow developers to create hierarchical structures of classes and reuse code efficiently. In our previous article “Inheritance in Java,” we discussed the concept of inheritance, its advantages, and disadvantages. In this article, we will delve deeper into the topic of inheritance and explore the various types of inheritance in Java. Understanding these inheritance types and their implementation is crucial for creating robust and scalable Java applications. So, whether you are a novice or an experienced Java developer, this article will help you to master the different types of inheritance and use them effectively in your programs.
Java supports several types of inheritance, including:
Single Inheritance
Single Inheritance is a type of inheritance in which a child class extends a single parent class. The child class inherits all the non-private members (fields, methods) of the parent class. Let’s take an example to understand Single Inheritance in Java:
//Parent Class class Study { public void exam() { System.out.println("Don't Stress! Do your best!"); } } //Child Class class Trigger extends Study { public void motivate() { System.out.println("There is No Substitute for your Hard Work!"); } } //Main Class public class SingleInheri { public static void main(String[] args) { Trigger ob = new Trigger(); ob.exam(); //Inherited method from parent class ob.motivate(); //Child class method } }
In this example, the Trigger
class is a subclass of Study and inherits the exam()
method from it. It also has its own unique motivate()
method.
“Save the above code as “SingleInheri”. After saving it, compile and run the code. You will get the below output.
Don't Stress! Do your best! There is No Substitute for your Hard Work!
To practice more programs on single inheritance, please refer to the article titled “Practice Problems on Inheritance in Java“.
Multilevel Inheritance:
Multilevel inheritance is a type of inheritance in which a derived class is created from another derived class. In other words, it involves creating a hierarchy of classes where a class is derived from a derived class. It is like building a chain of inheritance where each class inherits from the one before it.
For example, consider the following scenario: We have a class called Study
which has a method called preparation()
. Now we create a derived class called InternalExams
which inherits from Study
and has a method called internals()
. Finally, we create another class called ExternalExams
which inherits from InternalExams
and has a method called externals()
. Now, ExternalExams
has access to all the methods of InternalExams
, Study
, and itself.
class Study { public void preparation() { System.out.println("I am Studying"); } } class InternalExams extends Study{ public void internals() { System.out.println("For Internal Exams"); } } class ExternalExams extends InternalExams { public void externals() { System.out.println("which help me prepare for the external exams."); } } public class MultiInheri { public static void main(String[] args) { ExternalExams e = new ExternalExams(); e.preparation(); // Inherited from Study class e.internals(); // Inherited from InternalExam class e.externals(); // Defined in ExternalExams class } }
“Save the above code as “MultiInheri”. After saving it, compile and run the code. You will get the below output.
I am Studying For Internal Exams which help me prepare for the external exams.
To practice more programs on multilevel inheritance, please refer to the article titled “Practice Problems on Inheritance in Java“.
Hierarchical Inheritance
Hierarchical Inheritance in Java is a type of inheritance where a single class is inherited by multiple sub-classes, i.e., multiple sub-classes are created from a single base class. Each sub-class inherits the properties and behaviors of the base class, and can also add its own properties and behaviors.
Here is an example code for Hierarchical Inheritance:
class Animal { public void eat() { System.out.println("Eating..."); } } class Dog extends Animal { public void bark() { System.out.println("Woof!"); } } class Cat extends Animal { public void meow() { System.out.println("Meow!"); } } public class HierarchicalInheri { public static void main(String[] args) { Dog d = new Dog(); d.eat(); d.bark(); Cat c = new Cat(); c.eat(); c.meow(); } }
In the above code, the base class Animal
is inherited by two sub-classes Dog
and Cat
. Both Dog
and Cat
inherit the eat()
method from the Animal
class, and also have their own unique methods bark()
and meow()
respectively. This demonstrates how Hierarchical Inheritance allows for code reuse and organization, as well as the ability to add unique behaviors to each sub-class.
“Save the above code as “HierarchicalInheri”. After saving it, compile and run the code. You will get the below output.
Eating... Woof! Eating... Meow!
To practice more programs on hierarchical inheritance, please refer to the article titled “Practice Problems on Inheritance in Java“.
Multiple Inheritance (achieved through interfaces):
Multiple Inheritance is a feature of object-oriented programming languages where a class can inherit properties and behavior from multiple parent classes. In other words, a class can have more than one immediate parent class, and thus, can inherit features from each of them.
However, Java does NOT support Multiple Inheritance directly, i.e., a class cannot inherit properties and behavior from multiple parent classes. This was a design choice made by the creators of Java to avoid ambiguity and complexity that could arise due to the Diamond Problem.
The Diamond Problem arises when two parent classes of a class have a common method with the same name and signature, and the child class tries to inherit both methods. This creates ambiguity, and it’s not clear which of the methods should be used.
To overcome this problem, Java provides interfaces, which are similar to abstract classes that define a set of methods without an implementation. A class can implement multiple interfaces, and thus, can inherit properties and behavior from each of them. This approach is called “Interface Inheritance” and is a way of achieving some of the benefits of Multiple Inheritance without the complexity and ambiguity of the Diamond Problem.
If you want to learn more about interfaces in Java, please refer to our “Interface in Java” article. Additionally, if you’re interested in learning about abstract classes in Java, please check out our “Abstract Class in Java” article.
Hybrid Inheritance:
Hybrid inheritance is a combination of multiple types of inheritance, such as multiple inheritance and hierarchical inheritance. It is not directly supported by Java, but can be achieved through a combination of inheritance and interfaces.
In general, the type of inheritance used depends on the requirements of the application and the relationships between classes. Single inheritance is the most common type of inheritance used in Java, as it is simple and straightforward to use. Multiple inheritance and hybrid inheritance can be more complex and difficult to manage, but may be necessary in some cases to achieve the desired functionality.
In conclusion, we have learned about the different types of inheritance in Java, including Single, Multilevel, Hierarchical, and Hybrid Inheritance. Each type has its own advantages and disadvantages and can be used in different scenarios depending on the program’s requirements. We also discussed how Java does not support multiple inheritances but provides a workaround through interfaces. We hope that this article has provided a solid understanding of inheritance in Java. For more practice on problems related to inheritance, please refer to the “Practice Problems on Inheritance in Java” article. And to learn more about interfaces in Java, refer to our next article “Interfaces in Java.”