Inheritance in Java
Inheritance is a mechanism wherein one class inherits the property of another. In inheritance, one class can adopt the methods and behaviour of another class. It is a useful practice if you want to avoid writing the same piece of code repeatedly.
What is the Inheritance in Java?
Inheritance is an object-oriented programming concept in which one class acquires the properties and behaviour of another class. It represents a parent-child relationship between two classes. This parent-child relationship is also known as an IS-A relationship.
Let us discuss a real-world example that helps us understand the need and importance of inheritance.
Imagine you have built a standard calculator application in Java. Your application does addition, subtraction, multiplication, division, and square root.
Now, you are asked to build a scientific calculator that performs additional operations like power, logarithms, trigonometric operations, etc., along with standard operations. So, how would you go about writing this new scientific calculator application?
Again, would you write the code for all standard operations, like addition, subtraction, etc.? No, you have already written that code. Right? You would want to use it in this new application without writing it repeatedly. Using inheritance in this case, you can achieve this objective. Let us see how in the below section.
Why Use Inheritance in Java?
In the real-world scenario we discussed in the introduction, applying inheritance would give us reusability. You could use the code you have written for the standard calculator in the new scientific calculator without writing the same again.
You will apply inheritance and inherit the standard calculator class, which contains code for standard operations for the new scientific calculator class. This new class now inherits all the calculator operations of the old class. You can now write methods for additional operations in this new class. This is the primary use case of inheritance in Java.
Another use of inheritance is that using inheritance method overloading & method overriding can be achieved ( polymorphism).
Inheritance concept
Subclass
A subclass, also known as child class or derived class, inherits another class's properties and behaviourss. So, if A and B are two classes and if B class inherits A class, then the B class is called the subclass.
Superclass
A superclass, also known as parent class or base class, is the class whose properties and behaviors are inherited by the subclass. So, if A and B are two classes and if B class inherits A class, then A class is called the superclass.
The properties and behaviour of a class are the attributes and methods present in the class. So, when it inherits the superclass, the subclass will get all the attributes and methods of the superclass.
But here, it gets a little tricky. The access modifiers private, public, protected, and default affect how the attributes and methods of the superclass are inherited by the subclass. We shall see how in the next sections of the article.
How to achieve/implement inheritance in Java?
Let us list a few terms and keywords generally used in inheritance.
-
Subclass: The class that inherits the attributes and methods of another class.
-
Superclass: The class whose attributes and methods the subclass inherits.
-
Extends: The subclass uses the keyword to inherit the superclass.
-
Reusability: The methods and attributes of the superclass can be reused in the subclass because of inheritance, this is called reusability.
Example of Inheritance in Java
Below is a simple JAVA program that illustrates how inheritance works:
Output:
Explanation:
- A class Superclass with method methodSuper() and another class Subclass with method methodSubclass() are written.
- Inherit the Superclass to Subclass using the extends keyword.
- Create an object for SubClass obj in the main method
- Since the SuperClass is inherited from the SubClass, the methodSuper() of SuperClass is now part of the SubClass.
- So, SubClass now has two methods, methodSuper() and methodSubclass(), which can be called using the obj Object
Types of Inheritance in Java
Five different types of inheritances are possible in Object-Oriented Programming.
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Let us look at each of them and with examples.
1. Single Inheritance in Java
This is the simplest form of inheritance, where one class inherits another class.
Ex: Below is a simple JAVA program that illustrates the Single Inheritance
Output:
Explanation:
- Bird Class is written with a method fly().
- The Parrot Class inherited the Bird Class using extends keyword.
- An object of type parrot is created in the main method of the main class. This object can be called the fly() method of the Bird class as it inherits the Bird class.
2. Multilevel Inheritance in Java
This is an extension to single inheritance in Java, where another class again inherits the subclass, which inherits the superclass. The figure below makes this inheritance clear.
Here, Subclass 2 can again be inherited by another class, Subclass 3. This can continue, forming a linear chain of inheritances. This is called Multilevel inheritance in Java.
Ex: Below is a simple JAVA program that illustrates the Multilevel Inheritance
Output:
Explanation:
- Here in this program, the chain of inheritance is the Bird class is inherited by the Parrot class, and the Parrot class is, in turn, inherited by the SingingParrot class.
- The object of SingingParrot created in the main method of java will be able to access the methods whatCanISing(), whatColourAmI(), fly() as they are all inherited by SingingParrot class using multilevel inheritance in java.
3. Hierarchical Inheritance in Java
In Hierarchical inheritance, a single superclass is inherited separately by two or more subclasses. The figure below illustrates this inheritance:
Ex: Below is a simple JAVA program that illustrates the Hierarchical Inheritance
Output:
Explanation:
- The superclass Bird is inherited separately by two different subclasses Parrot and Crow.
- The Parrot object can call its method whatColourAmI() and also the fly() method, which is inherited from the superclass Bird.
- The Crow object can call its method whatColourAmI() and also the fly() method, which is inherited from the superclass Bird.
4. Multiple Inheritance in Java
In Multiple Inheritance, a single class inherits from two different superclasses. Multiple Inheritance for classes is Invalid in Java.
Consider that there is a superclass1 named A, and the A class has a method testMethod(). Consider there is a superclass2 named B, and B class has a method testMethod(), which has the same name as class A. Now, lets us suppose that classes A and B are both inherited by a subclass named C. And if testMethod() is called from the subclass C, an Ambiguity would arise as the same method is present in both the superclasses.
Note: The above program is just to understand that multiple inheritance of classes is invalid in Java.
5. Hybrid Inheritance in Java
Hybrid inheritance is a combination of hierarchical inheritance and multiple inheritance. This is also not possible and is Invalid in Java. The figure below illustrates hybrid inheritance in Java.
Which Inheritance is Not Supported in Java?
Java does not support multiple inheritance, which means a class cannot inherit characteristics (methods and fields) from more than one superclass. This is done to avoid confusion and complexity caused by the "Diamond Problem."
Consider a real-life example where a child can inherit traits from both parents, but if there's a conflict, such as the child's eye color being brown from the mother and blue from the father, it's unclear which color the child should inherit.
Similarly, in Java, if class B and C inherit from A, and both override the same method from A, then if a class D inherits from both B and C, it's unclear which method D should inherit, hence creating a "diamond" shaped inheritance diagram. To avoid such complications, Java prohibits multiple inheritance.
Conclusion
We have reached the end of the article. Below are some of the important points to keep in mind when working with inheritance.
- Inheritance helps in code reusability.
- Inheritance represents the IS-A relationship between classes.
- The keyword extends is used to perform inheritance.
- Multiple inheritance and Hybrid inheritance are not valid in Java, although they can be performed on interfaces.
- The super keyword can be used to access the parent class's variables/methods/constructor.