Inheritance is one of the essential parts of Object-oriented programming(OOP). As suggested by the name, it is the process of inheriting some or all properties of your ancestors or preceding generations. The same applies to Inheritance in Java. It is the process by which a subclass inherits some methods or fields of the superclass.
Let’s discuss what Inheritance is and how many types of inheritance are there in Java. Also, know how you can perform multiple inheritance in Java.
Inheritance in Java can be defined as a mechanism through which a sub-class acquires properties of the superclass. To understand it better, consider an example:
You, as a child, would have acquired some properties from your parents. You may have some shade of hair like your mother or the same color of eyes as your father.
In the same way, your parents would have also inherited some of their features from your grandparents. So, This is how you have also indirectly inherited some of the properties of your grandparents.
In the same way, inheritance works in Java as well. One class derives properties from an existing class.
Now we hope you are aware of what inheritance is. Let’s proceed with some terms that you will come across while learning about inheritance.
Class is a group of some elements and methods which share common properties.
A superclass is a class whose properties are inherited by other classes.
The subclass is the class that inherits properties from the superclass. However, other than the methods from the superclass, the subclass can also have its own fields and methods.
While talking about inheritance, you may have come across the term reusability. With the help of the reusability feature, a new class can use all the properties and methods of an existing class. With this, a code can be reused again and optimized properly.
While inheriting a class from an existing class, you need to use the keyword “extends.”
Class [name of derived class] extends [name of base class]
Fields and methods
So, now that you are aware of how you can extend a class, here are the types of inheritance that you can use in Java.
When a single class extends a single superclass, it is called single inheritance. In simple terms, single inheritance occurs when there is only one derived class and one superclass. The derived class can then use methods and fields of the superclass without any issues.
As the name suggests, this inheritance also has multiple levels. In this type of inheritance, a derived class will first derive properties from the base class. This derived class will then behave as a base class for another derived class and so on.
Now the next type of inheritance is Hierarchical inheritance. In this type, there is only a single base class with multiple derived classes. It simply means that all the derived classes will acquire properties of a single base class.
Hybrid inheritance is a combination of different types of inheritance mentioned above. Just like multiple inheritance, hybrid inheritance is also not supported in Java. To achieve hybrid inheritance in Java, one needs to use interfaces.
Though multiple inheritance is not supported in Java you can still inherit multiple classes using interfaces. Multiple inheritances mean that there is one derived class and multiple base classes. With Java, this is not possible using the extended keywords but can be done using interfaces.
For interfaces, you need to use the keyword “implements.”
Here we have explained to you some concepts about inheritance in Java to give you a clearer understanding of the concept.
In inheritance, there can be multiple subclasses. One base class can have multiple sub-classes. However, there can be only one superclass in inheritance.
This is the major reason why Java does not support multiple inheritances. However, you can perform multiple inheritances via interfaces.
There is no superclass for the object class. Other than that, each derived or sub-class has one superclass. You can also consider it a single inheritance. However, if there is no explicit superclass available, every class will automatically become a sub-class of the object class.
Now, when you define fields and methods, you keep some of them private, public, or protected. When it comes to private methods and fields of the superclass, you can not extend or inherit them in your derived class. You can only inherit the public and protected methods in the sub-class.
While inheriting, a derived class is allowed to inherit all the fields and methods of the base class. However, one must know that a constructor is not a member of your class. So, the subclass will not be able to inherit the constructor. However, you can still invoke the constructor from the subclass.
Inheritance in java is one of the important concepts you must understand as a programmer. Inheritance will help to achieve code reusability and optimize the code properly. Not only this, but inheritance can also give you a better structure of the code which is easier to understand.
As a Java programmer, you must have written a lot of programs like printing a series of numbers, strings, or Fibonacci series in Java. But, What about complex programs with multiple functions and multiple classes?
Well, in that case, excelling inheritance in java would be of great help.
We hope the post has given a clearer picture to you of what inheritance is and how you can implement multiple inheritance in Java.
To Read More Tech Blogs Visit: Technical Nick