Inheritance is an object-oriented programming (OOP) concept by which the properties and behaviors from a parent class are passed on to a child class.
In day-to-day life, when something gets passed from a parent to a child, it is said that child inherits from their parent. Similarly, in Java, when some properties or behaviors get passed from parent class to the child class, it is said that child class inherits from a parent class.
Child classes can have properties and methods of its own as well.
They can also override the behaviors (methods) of the parent class. This is called polymorphism (another OOP concept).
We can implement inheritance by using the extends keyword.
Syntax
Here’s the basic syntax for implementing inheritance:
public class Child extends Parent {
}
Example
public class Vehicle {
private int numWheels;
private String engineNum;
private String maker;
private String color;
public int getNumWheels() {
return numWheels;
}
public void setNumWheels(int wheels) {
this.numWheels = wheels;
}
public String getEngineNum() {
return engineNum;
}
public void setEngineNum(String engineNum) {
this.engineNum = engineNum;
}
}
In the code above, Vehicle is a class that has properties and behaviors. This is more of a generic class.
While creating a specific class, for example, Car which has these properties and behaviors and some extra properties, instead of defining all these properties again in the new class, we can make use of inheritance to pass on the properties defined in Vehicle class and access them in Car class. This would make code written in Vehicle class reusable.
Similarly, we can make another class, for example, Airplane, which can access the properties present in Vehicle class.
Here’s a Car class which inherits Vehicle class:
public class Car extends Vehicle {
private int airbagCount;
public int getAirbagCount() {
return airbagCount;
}
public void setAirbagCount(int airbagCount) {
this.airbagCount = airbagCount;
}
}
Below is how you can access properties and methods of parent class:
public static void main(String args[]) {
Car car = new Car();
car.setNumWheels(4);
System.out.println(car.getNumWheels());
car.setAirbagCount(2);
System.out.println(car.getAirbagCount());
}
Types of Inheritance
Single Inheritance
Single inheritance is when a child class inherits from a parent class.
public class Parent {
}
public class Child extends Parent {
}
Multilevel Inheritance
Multilevel inheritance is when a child class inherits from a parent class which in turn inherits from another parent class.
public class Parent {
}
public class Child extends Parent {
}
public class GrandChild extends Child {
}
Hierarchical Inheritance
Hierarchical inheritance is when a child class inherits from a parent class and there is another sibling class that also inherits from the same parent class.
public class Parent {
}
public class Child extends Parent{
}
public class Sibling extends Parent{
}
Advantages of Inheritance
- Code reusability: Same properties and methods of a class can be used by inheriting that class.
- Lower maintenance cost: If a piece of code needs to be updated, it can be done at minimal place if inheritance is used.
- Easier to add new features: If a same new feature needs to be added to multiple classes, through inheritance it can be added in parent class and all the child classes inheriting this parent class would get that feature instantly.