Inheritance in Java
The word 'inherit' means receive.
Imagine a parent and a child, how a child's genetical nature, color would be?
The child's characteristics(color,height) will depend upon the child's parents. Right?
We can say the above as the child inherits the properties of a parent.
Imagine child as a class and parent as a another class. If we create methods and variables for parent class, we can use same methods and variables in the child class without re-writing methods. i.e reusability of codes. This is called as inheritance.
Inheritance is the concept of deriving a parent class property from a child class.
Here derived class - sub-class and parent class - super class
To derive a class from parent-class, we use extends
General Syntax
class subclass_name extends superclass_name{
//body of the class
}
Types of Inheritance
- Single inheritance
- Multi-level inheritance
- Hierarchial inheritance
Single Inheritance
When a child class inherits a parent class, it is known Single inheritance.
Example
class MotorCycle{
MotorCycle(){
System.out.println("It has two wheels");
}
void engine(){
System.out.println("Petrol Engine");
}
}
class RoyalEnfield extends MotorCycle{
void mileage(){
System.out.println("It has 20 km mileage.");
}
public static void main(String args[]){
RoyalEnfield re=new RoyalEnfield();
re.mileage();
re.engine();
}
}
Try In EditorOutput
It has two wheels
It has 20 kms mileage.
Petrol Engine
Order of execution
At first, Parent's constructor i.e. MotorCycle() in above example is executed.
Next, Child's constructor i.e.RoyalEnfield()
Then as per the call of methods and variables i.e. re.mileage().
Multi-level Inheritance
Inheriting a parent class by a child and the child again is inherited by a grand-child is known Multi-level inheritance.
Example
class Vehicle{
Vehicle(){
System.out.println("It is used for transporting people and goods.");
}
}
class FourWheeler extends Vehicle{
FourWheeler(){
System.out.println("It contains four wheels.");
}
}
class Car extends FourWheeler{
Car(){
System.out.println("It can carry only small number of people.");
}
public static void main(String args[]){
System.out.println("Properties and usage of a car are ");
Car car=new Car();
}
}
Try In EditorOutput
Properties and usage of a car are
It is used for transporting people and goods.
It contains four wheels.
It can carry only small number of people.
Hierarchial Inheritance
Inheriting a parent class by many childs is known Hierarchial inheritance.
Example
class MotorCycle{
MotorCycle(){
System.out.println("It has two wheels");
}
void engine(){
System.out.println("Petrol Engine");
}
}
class RoyalEnfield extends MotorCycle{
void mileage(){
System.out.println("It has 20 km mileage.");
}
}
class Yamaha extends MotorCycle{
void mileage(){
System.out.println("It has 30 km mileage.");
}
public static void main(String args[]){
System.out.println("About RoyalEnfield :");
RoyalEnfield re=new RoyalEnfield();
re.mileage();
re.engine();
System.out.println("About Yamaha :");
Yamaha yh=new Yamaha();
yh.mileage();
yh.engine();
}
}
Try In EditorOutput
About RoyalEnfield :
It has two wheels
It has 20 kms mileage.
Petrol Engine
About Yamaha :
It has two wheels
It has 30 kms mileage.
Petrol Engine