Inheritance in Java
Inheritance is a mechanism wherein one class inherits the property of another. In inheritance, one class can adopt the methods and behavior 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 behavior 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, which does additional operations like power, logarithms, trigonometric operations, etc., along with standard operations. So, how would you go about writing this new scientific calculator application?
Would you write the code for all standard operations, like addition, subtraction, etc., again? No, you have already written that code. Right? You would want to use it in this new application without writing it all over again. 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. And you can now write methods for the 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).
A subclass, also known as child class or derived class, is the class that inherits the properties and behaviors of another class. So, if A and B are two classes and if B class inherits A class, then the B class is called the subclass.
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 behavior of a class are the attributes and methods present in the class. So the subclass will get all the attributes and methods of the superclass when it inherits the superclass.
But here, it gets a little tricky. The access modifiers private, public, protected, and default affect the way the attributes and methods of the superclass are inherited by the subclass. We shall see how in the further sections of the article.
How to achieve/implement inheritance in Java?
Let us list out a few of the 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:
- 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 to 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
There are five different types of inheritances that 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
- 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 is able to call 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 below figure makes this inheritance clear
Here Subclass 2 can again be inherited by another class, Subclass 3. This can keep on going 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
- 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 below figure illustrates this inheritance:
Ex: Below is a simple JAVA program that illustrates the Hierarchical Inheritance
- 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 there is a superclass1 name A, and A class has a method testMethod(). And 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 for understanding 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 Invalid in Java. The below figure illustrates the 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. The main reason behind this is to avoid confusion and complexity caused by the "Diamond Problem".
Consider a real-life example where a child can have traits from both parents, but if there's a conflict, such as eye color from the mother is brown and from the father is blue, 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.
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.
- super keyword can be used to access the variables/methods/constructor of the parent class.