Inheritance is a concept of using the methods and attributes of one class(superclass/base class) into another(child class/subclass) with the help of the “extends” keyword.
The concept of inheritance allows us to achieve code reusability and method overriding.
Let’s see the syntax for Inheritance:
class SuperClass
{
//methods and fields
}
class SubClass extends BaseClass
{
//methods and fields
}
Let’s see an example of Inheritance
package corejava;
class SuperClass{ /*superclass method*/
void name() {
System.out.println("I am Superclass");
}
void methodExample() {
System.out.println("I am an Inheritance example");
}
}
class SubClass extends SuperClass{ /*subclass method*/
void name() { //overridden method
System.out.println("I am Subclass");
}
}
public class Oop {
public static void main(String[] args) {
SubClass object1=new SubClass();
object1.methodExample(); //inherited from superclass
object1.name();
}
}
Output:
I am an Inheritance example
I am Subclass
Types of Inheritance
Types of Inheritance | Allowed in Java? |
Single Inheritance | Yes |
Multilevel Inheritance | Yes |
Multiple Inheritance | No |
Hierarchical Inheritance | Yes |
Hybrid Inheritance | No |
Now let’s see each category of inheritance one by one:
Single Inheritance
In Single Inheritance, a parent class/super class is extended/inherited by one and only one child class/base class.
Let’s see how to implement single inheritance.
class ParentClass
{
}
class ChildClass extends ParentClass
{
}
Multilevel Inheritance
In Multiple Inheritance, we can have multiple levels of single inheritance, that is a parent class can be inherited by an intermediate child class which itself can be inherited by a child class, and so on…
Let’s see how to implement multiple inheritance.
class ParentClass
{
}
class IntermediateChildClass extends ParentClass
{
}
class ChildClass extends IntermediateChildClass
{
}
Hierarchical Inheritance
In Hierarchical Inheritance, a superclass can be inherited by multiple sub-classes.
Let’s see how to implement hierarchical inheritance.
class ParentClass
{
}
class ChildClass1 extends ParentClass
{
}
class ChildClass2 extends ParentClass
{
}
Multiple Inheritance
In Multiple Inheritance, multiple superclasses can be inherited by a single subclass.
As we discussed in the above table, multiple inheritance is not allowed in Java.
But why is it so??
Let’s explore this with an example.
Let us consider we have two classes ParentClass1 and ParentClass2 and each of these have a different implementation of method1() and a child class inherits from both ParentClass1 and ParentClass2. Now which implementation of method1() do you think the child class will have??
Confused right…This is the reason why multiple inheritance is not allowed in Java.
But if you need to apply multiple inheritance in Java, you can do so with the help of interfaces.
Let’s see how…
As you know, interfaces only contain declarations and not implementations and hence you can implement multiple interfaces because finally the implementation will be provided in the child class.
This is how multiple inheritance is possible in Java but with the help of interfaces.
Hybrid Inheritance
Hybrid Inheritance is the combination of two or more types of inheritance.
As multiple inheritance is not allowed in Java, hybrid inheritance is also not allowed but if needed you can use it with the help of interfaces.
Wrapping it up…
In this blog, we have discussed Inheritance in Java which is one of the most important principles of Object Oriented Programming. But as we know Java is vast and its vastness can sometimes be scary to new learners.
But not to worry as I’ll be covering most of the important topics that you can face while developing a website.
If your goal is to get a professional certification for Java you can refer to this course by Edureka and for an overall understanding of Java web development, you can refer to this course by Udacity.