Inheritance.
The idea is to adopt or inherit the properties (functions , variable , constants also constructor )
Of a particular class to another class;
The class which is adopting the properties of another class is said to be the child class ;
The class which will be inherited by another class can be called as the parent class;
This parent and child class comes from real world examples like : a mother is good at drawing and good at singing :- so their child can inherit both of her qualities and also can have his/her own quality such as the child can be good at sports too.
The exact same way inheritance works the same in java .
Let’s make an outlook of this inheritance .
Single level inheritance .
Parent class:-
Child class:-
Test class ➖
We can see that the parent class has the properties such as eats , sleeps, has a name and a age .
But the child class does not have one but still it can access the parents class properties by inheriting it with the help of extends keyword.
Multilevel Inheritance :-
This idea is same as the parent and child relation , only we can imagine more that the child has a grandparent
, so the child now can adopt the properties of both parent and grandparent .
Example:-
The topmost parent class :-
The next parent class :-
The lowermost child class :-
The test class :
In this code parent class extends grandparent class and child class extends parents class .
So both the parents and child class do have the properties of the grandparent class.
Now we can see in the above code that grandparents also have a property of snoring .
But children usually does not snore .
So we can also redefine the property (here function)
In the child class.
Example :- Grandparent class
Child class :-
Test class :-
Output
Here we can see for the child snors function return false and for grandparent is true ,
Because we have redefined the snores function in child for the child class ,
This redefining of the same function in the child class as the parent class for different purposes is known as function overriding .
As the child class already has a snors function it wont call the parents snors function.
How does constructors works in inheritance :-
The constructors are for the purpose of initialising the initial parameters .
Thus in inheritance first the parent’s constructor is called then the child’s constructor is called on creating a child class object .
Get Saptarshiok’s stories in your inbox
Join Medium for free to get updates from this writer.
Example
Now on creating a object of a class :-
Output:
This is how constructors are called in inherited classes , but it does not mean that the child class cannot have a different constructor from parent class:
Now the child class also have a different constructor of its own :
Calling the parameterized child constructor:
Output:
Here although the constructor is not similar to the parent class, the parent class constructor will be called before the child’s constructor.
Some points :
- No static functions can be accessed by any child class .
- Static functions can only be used within the class .
- Child class also cannot access private properties of the parent class.
super Keyword:
This is a keyword that refers to the parent class.
In every constructor call in child class the reason it first executes the parent class constructor because there is a default super keyword written in the beginning of every constructor.
For example;
This super(); keyword is already present by default in the beginning of every class constructor .
This is responsible for calling the parent class constructor if there is any parent of that class.
- Super keyword cannot be written in the end of any constructor as it does not maintain the logic of calling the parent constructor first .
For example:
If we try to forcefully write a super keyword in the end of any constructor an error will occur .
But this super keyword is not for only calling constructors of parent classes. It gives reference to the parent class , thus it can be used to call functions of the parent class even after function overriding .
Parent class :
Child class :
Test class :
Output:
In this code even if there is a function overriding and normally the parent function snors() won’t be called , but explicitly using the super key word after the function overriding still the parent snors() class will be called .
Now it is understood that we can do single inheritance and multilevel inheritance .
But in java we cannot do multiple inheritance ,
Meaning we can’t inherit more than one class .
For example:
This extending more than one class is not allowed in java .
This is because if there are two similar functions in two parent classes compiler cannot understand which one to execute.
Overview :
Just for understanding in short :
For functions :
For constructors :





