Table of Contents
Java Inheritance :
Inheritance is acquiring the properties of parent class in child class. Re-usability is the important aspect of programming.
By extending a class you can use the properties and also add new methods in child class.
There are 3 types of java inheritance
- Single Inheritance
- Multi-level Inheritance
- Hierarchical Inheritance
Single Inheritance:
In single inheritance there is a parent/base class and child/derived class.

Create a parent class with a add method accepting two variables.
class Parent {
void add(int x, int y) {
System.out.println("Result of addition is " + (x + y));
}
}
And acquire the add method and can also add additional methods into derived class as
class SingleInheritance extends Parent {
public static void main(String args[]) {
SingleInheritance singleInheritance = new SingleInheritance();
singleInheritance.add(1, 2);
singleInheritance.sub(2, 1);
}
void sub(int x, int y) {
System.out.println("Result of substraction is " + (x - y));
}
}

Multi Level Inheritance:
In multilevel inheritance three are more than one derived class i.e., a derived class is further derived.

class Parent {
void add(int x, int y) {
System.out.println("Result of addition is " + (x + y));
}
}
from which a intermediate class acquires properties
class Intermediate extends Parent {
void sub(int x, int y) {
System.out.println("Result of substraction is " + (x - y));
}
}
now we can again acquire the properties of intermediate class
class MultilevelInheritance extends Intermediate {
public static void main(String args[]) {
MultilevelInheritance multilevelInheritance = new MultilevelInheritance();
multilevelInheritance.add(1, 2);
multilevelInheritance.sub(2, 1);
multilevelInheritance.multiply(2, 3);
}
void multiply(int x, int y) {
System.out.println("Result of multiplication is " + (x * y));
}
}

Hierarchical Inheritance:
In hierarchical inheritance we can derive more classes from a single base class.

Create a base class
class ID {
String id() {
return "";
}
}
inherit in from multiple classes as follow
Consider a student class having id
class Student extends ID {
String id() {
return "Student 01";
}
}
Employee class with id
class Employee extends ID {
String id() {
return "Employee 01";
}
}
now we can check how this inheritance works in real time
class HierarchialInheritance {
public static void main(String args[]) {
ID i = new Student();
System.out.println(i.id());
i = new Employee();
System.out.println(i.id());
}
}

If any query in java inheritance tutorial let us know in comment section below.
Do like and share this tutorial for more interesting java tutorials.