What is Inheritance?
Inheritance is a significant feature of Object Oriented Programming language. Also, It is the mechanism by which child class acquires all the features (properties and behaviors) of a parent class.
So, The idea behind inheritance is that programmer can create new classes from the existing and older classes. Moreover, when developers inherit from an existing class, they can reuse properties and behaviors of a superclass and can add new methods and fields as well.
Why use inheritance in java
- The first use is of Method Overriding.
- The second use is Code Reusability.
To start with, these are few important terms –
The class that extends data members and methods of another class is known as the subclass, child class, or derived class.
This is the class whose data members and methods are used(inherited) by another class. This is also known as a superclass, parent class or Base class.
extends is the keyword which is used to inherit the features of a class
Types of inheritance in java –
Before we proceed further, this is very important to understand the types of inheritance which Java supports. Hence, this is the list –
Syntax of Java Single Inheritance:
classSubclass-name extends Superclass-name
//methods and fields
Here in Syntax extends keyword means that you are creating a new class that derives and inherit features from an existing or old class. The meaning of “extends” is to increase the functionality more.
In the terminology of Java, a class which is inherited is called superclass and the new class is called subclass.
Single Inheritance –
Single Inheritance includes only two classes. One is parent class and other is child class. Child class going to inherit all features from the parent class.
Multilevel Inheritance –
In multiple inheritance, we have more than two classes and that class is derived from another class. There is more than one class which act as the parent class.
Hierarchical Inheritance –
Hierarchical inheritance includes more than one child classes or we can say that more than one child class have one same p[arent class from where child class can inherit all features and functionalities from a superclass.
In java programming, multiple and hybrid inheritance is supported through interface only. We will publish the blog interface soon.
Why multiple inheritance is not supported in java?
Java does not support multiple inheritance to trim down the complication and simplify the language. It makes the code more complex. It creates error during the performance of operations like constructor chaining, casting etc, so we avoid using multiple inheritance in Java to keep things more simple and straightforward.
This is all about Inheritance in Java programming language. I hope this article helps you understand the concept of Inheritance better.
By any chance, if you want to see the video tutorial on the same topic, here is the YouTube video link for you –