Inheritance in Java is a mechanism to derive or inherit the property and behavior from one class to another class. The class that is used to inherit the property and behavior is the parent class or base class and the class that inherited the property is the child class or subclass. This inheritance OOPS concept represents an IS-A relationship or parent-child relationship.
To understand inheritance better let's take the father and son example. Imagine we have two classes Vehicle and Benz. Vehicle class contains properties like tires, seat, and engine so if the Benz class inherits the Vehicle class then Benz automatically holds all the property of the Vehicle class we don't need to rewrite the same logic in the child class. Benz will have some special property and this can be added to it.
Why Need Inheritance In Java?
- Code reusability
- Method overriding
Inheritance Java Syntax
- A child class can inherit only protected and public data variables and methods from the parent class. Private data variables and methods can't be inherited.
- Till Java 7 multiple inheritance was not supported. A subclass is not allowed to inherit the same method from two different parent
- Parent class constructor can't be inherited by the child class. The child should use the super() keyword to call the parent class constructor in Java.
- Data variables can't be overridden by the child class.
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
Single Inheritance Example
public class Calculator{
int divider = 10;
void dividerCal(int a, int b) {
System.out.println("Division "+ (a+b)/divider);
}
void add(int a, int b) {
System.out.println("Additon "+ (a+b));
}
}
class Child extends Calculator{
void display() {
//super keyword used to access parent properties
super.divider = 5;
super.dividerCal(3, 10);
}
public static void main(String args[]) {
Child obj = new Child();
obj.display();
//Child class object used to call parent properties
obj.add(5, 4);
}
}
Multiple Inheritance Example
//Parent1
interface CPU{
void powerOn();
void openDisk();
}
//Parent2
interface Monitor{
void powerOn();
void screenSaver();
}
class Computer implements CPU, Monitor{
@Override
public void screenSaver() { }
@Override
public void powerOn() { }
@Override
public void openDisk() { }
}
- Class A will act as the grandparent of class X
- Class X will act as the grandchild of class A
- Class B is child to class A and parent to class X
Multilevel inheritance Example
class ElectricalDevice{
void powerSupply() { }
void circuitBoard() { }
}
class Computer extends ElectricalDevice {
void camera() { }
void internet() { }
}
class SmartPhone extends Computer{
void phoneCall(){ }
public static void main(String args[]){
SmartPhone obj = new SmartPhone();
//Grandparent properties
obj.circuitBoard();
//Parent properties
obj.camera();
obj.internet();
}
}
Hierarchical Inheritance Example
class Animal{
void run() { }
void sound() { }
void eat() { }
}
//child 1
class Dog extends Animal{
void bark() {
super.sound();
}
public static void main(String args[]) {
Dog obj = new Dog();
obj.eat();
obj.run();
}
}
//child 2
class Tiger extends Animal{
void roar() {
super.sound();
}
public static void main(String args[]) {
Tiger obj = new Tiger();
obj.eat();
obj.run();
}
}
- Multilevel and Hierarchical inheritance
- Hierarchical and Single inheritance
- Single and Multiple inheritance
- Multiple and Multilevel inheritance