Java Inheritance With Examples

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
Within the child class, you can access parent class properties using the super keyword. In other classes where the child class is being used, you can access the parent properties using the child object. 

Inheritance Java Syntax 

class <child-class> extends <parent-class>{
}

What are the inheritance limitations?
  •  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.

Types of Inheritance
  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Hybrid inheritance
Single inheritance: A child class inherits property and behavior from only one parent is known as single inheritance. From the below java single inheritance, we can understand that class B (child class) inherits the property and behavior from class A(parent class).

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: A single child class inherits multiple parent class property and behavior. From the below java multiple inheritance, we can understand that class B inherits the property of both class A and class C.

Note: Multiple inheritance can be achieved using Interface in Java because Java doesn't support multiple inheritance.

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() { }

}


Multilevel inheritance: In  Java multilevel inheritance, a child class will have a child means the child class of some other class can act as a parent class when its property and behavior get inherited by another class. Very important! the last child will inherit all the property and behavior of all the previous parents.

For example, Let's consider class A as a parent class and class B as a child of A. Now if any other class inherits class B then class B is also a parent of class X. 

Key terms,
  • 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: A single parent class will have multiple child classes so the property and behavior of one class will be inherited by multiple children. From the Java hierarchical inheritance example, we can understand the concept easily,

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();

}

}


Hybrid Inheritance: This inheritance is a combination of two or more inheritance we discussed above. 
  • Multilevel and  Hierarchical inheritance
  • Hierarchical and Single inheritance
  • Single and Multiple inheritance
  • Multiple and Multilevel inheritance

Post a Comment

Previous Post Next Post
B