The meaning of Inheritance is "Comes from Parents".
Here we will learn how inheritance is helpful in JAVA.
Into Object oriented programing, Inheritance is a feature which is helpful to use the methods of parent class into the child class, or can call methods of parent class using the child class reference.
For example we have a class Car here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class Car{ public void carHaveSeats(){ } public void startEngine(){ } public class shiftGear(){ } } |
A car can be treat as a Super class.
Into this super class we have three methods:
1. carHaveSeats
2. startEngine
3. shiftGear
Now if we want to Make sub classes of cars it can be:
1. AutomaticCar
2. ManualCar
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class AutomaticCar extends Car{ @Override public void shiftGear(){ //Automatic shift } public void playMusic(){ } } |
1 2 3 4 5 6 7 8 9 | public class ManualCar extends Car{ @Override public void shiftGear(){ //Manual shift } } |
Here ManualCar and AutomaticCar class have one override method i.e shiftGear which is comes from Parent class i.e. Car
As we know that ManualCar and AutomaticCar is child class of Car, so they have 2 methods also which are derived from their Parent class i.e. Car.
1. carHaveSeats
2. startEngine
so with the help of Child class object, we can call these two derived methods also, and we don't need to override them. For example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ManualCar manualCar = new ManualCar(); manualCar.carHaveSeats(); manualCar.startEngine(); manualCar.shiftGear(); AutomaticCar automaticCar = new AutomaticCar(); automaticCar.carHaveSeats(); automaticCar.startEngine(); automaticCar.shiftGear(); Car car = new AutomaticCar(); car.carHaveSeats(); car.startEngine(); car.shiftGear(); |
As we can see from line number 1 to 11, we have created an object of Child class, call their methods. and we should not have confusion on that is if there is override method available, then that override method will be call otherwise derived method(Derived from Parent class) will be call.
Here shiftGear method is override method.
But into line number 13 we have a reference of car but object of AutomaticCar.
Can we do this? is there any error?
Yes, we can do this. It is the beauty of Inheritance and also it is the use of Abstraction into the Inheritance.
Here is the statement:
We can instantiate a reference variable of Parent class with the Child class object, but can not do vice versa.
So, The Car reference can call all it's method. and override methods will be call from the object, same as automaticCar reference.
But there is one difference between the car reference and automaticCar reference.
From automaticCar reference we can call
automaticCar.playMusic()
method, because playMusic() method belongs to AutomaticCar only
but from the reference of Car, we can not call
car.playMusic()
because, playMusic method is not belongs to Car class.
The conclusion is, references know only there methods definition, they don't care about the object type.
Java supports below type of Inheritance:
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
Single Inheritance:
Into Single Inheritance, One parent class have only one Child class.
Multilevel Inheritance:
Into multilevel Inheritance, Each child class have one Child class. But last child have not any child class.
Hierarchical Inheritance:
Into hierarchical Inheritance, one parent class have multiple Child classes.
We can see that above mentioned all type of Inheritance have one common things, i.e. No one can extends more then one class.
It is the base of Inheritance in JAVA. i.e. One class can extends only one class.
Use of super keyword:
A super keyword is use to hold the reference of super class. JAVA by default extends Object class, or we can say that Object class is a first super class in java.
So if we haven't extends any class, then by default extends Object class and super keyword holds Object class reference.
we can also call super class constructor from the child class constructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | public class Car{ private String name; public Car(){ } public Car(String name){ this.name = name; } public void carHaveSeats(){ } public void startEngine(){ } public class shiftGear(){ } public String getName(){ return name; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class AutomaticCar extends Car{ public AutomaticCar(){ } public AutomaticCar(String name){ super(name); } @Override public void shiftGear(){ //Automatic shift } public void playMusic(){ } } |
we can also call super class methods using super keyword
for example if we want to call shiftGear method of Car class from the AutomaticCar class then we can call it using super keyword
super.shiftGear();
Some documents mentioned that with the help of Interfaces, we can achieve multiple inheritance also
Oracle defines Multiple inheritance
I read many post about java inheritance but I really Impressed about your Writing Way and How to Express to words. It’s really helpful for me. Thanks for sharing. Keep writing
ReplyDeleteHi Kumar, thanks for your appreciation. It means a lot.
Delete