As we know, class is a way to represent any entity. Inside that, there are variables that store the information related to that entity. And, are also some functions that store the methods to modify and control the real-world entity.
For example, we have to represent a car. For this, we will make the car class and inside that, there are variables such as price, fuel type, engine, color, etc. to store the necessary information related to a car.
Also, there will be some methods to control some functioning while making the car, such as setColor(), setAlarm(), etc.
Now, it is required to make a class to represent another real-world entity, that is somehow related to the previous class that we have created. For example, now we have to represent Sports Car. As we know that sports car will be somehow related to a car, or it will be a car with some more special functions.
So, while making the class, we will need to add some more new functions and variables. And, we will also need to make those variables that are already available to us in the car class. This will lead to extra unnecessary work.
To prevent the unnecessary load, we have the concept of Inheritance in Java.
As we already have some of the functions and variables inside the car class, so there is no need to create them again inside the sports car. We can just create the sports car as the sub-class of the ‘car’ class due to which the previous variables will be inherited from its parent class and we will only create those functions and variables that are new in the sub-class.
The syntax for Inheritance –
Class SubClass extends Superclass{ }
We will understand it better with the help of the example given below –
public class Person{ private int age; private String name; public void setAge(int a) {age = a;} Public void setName(String n){ name = n;} public int getAge(){ return (age);} public String getName(){ return(name);} } Class Student extend Person{ public int rollno; public void setRollno(int r) {rollno = r;} public int getRollno(){return(rollno);} }
In the above program, we have created a class named person. After that, we want to create a class Student which is somehow related to the previous class person because every student is a person. So, the student will be the sub-class of person, which will hold the properties of the class Person as well as some additional properties.
There are some important properties that we need to remember about inheritance in Java –
- In Java programming, there can be only one superclass for each class, and each superclass can have an unlimited number of sub-classes. This means that multiple inheritances are not allowed in Java unlike other languages such as C++.
- Members with default accessibility in a superclass are not accessible by the subclass in other packages
- Private members of the superclass cannot be directly accessed by the subclass but can be indirectly accessed.
Types of Inheritance in Java
Java supports the following kind of inheritance –
- Single Inheritance – In this kind of inheritance, there is a superclass, and corresponding to that, there is a sub-class.
- Hierarchical Inheritance – In this kind of inheritance, there is a superclass, and corresponding to that superclass, there are more than one subclasses.
- Multilevel Inheritance – In this inheritance, there is a superclass, and corresponding to that superclass, there is a subclass. And further corresponding to that subclass, it also has its sub-class. This is continued.
So, in this article, we have learned about inheritance and the types of inheritance offered by Java. It is a very crucial feature of the language which is widely used by programmers.