Inheritance is an important pillar of OOP(Object-Oriented Programming). It is a technique in which we derive a new class form existing one. The new class is called sub class and existing class is called base class or super class
That means one class is allowed to inherit the features(fields and methods) of another class.
Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).
Sub Class: The class that extends the features of another class is known as child class, sub class or derived class.
Inheritance uses the extends keyword to create a derived class by reusing base class code.
Syntax:
class Subclass-name extends Superclass-name
{
//fields and methods
}
class Calculation {
int c;
public void addition(int a, int b) {
c = a + b;
System.out.println("Addition : "+c);
}
}
public class SubCalculation extends Calculation {
public void subtraction(int a, int b) {
c = a - b;
System.out.println("Substraction : "+c);
}
public static void main(String args[]) {
int x = 20, y = 10;
SubCalculation obj = new SubCalculation();
obj.addition(x, y);
obj.subtraction(x, y);
;
}
}
Output:
Addition : 30
Substraction : 10
In the following example we can observe two classes namely Calculation and SubCalculation.
Using extends keyword, the SubCalculation inherits the methods addition() of Calculation class.
There ara five types of inheritance:
But on the basis of class, there can be three types of inheritance available in java: single, multilevel and hierarchical inheritance.
And in java programming, multiple and hybrid inheritance is supported through interface only.
In single inheritance, subclasses inherit the features of one superclass. In the image below, class A serves as a base class for the derived class B.
class A
{
int a, b;
void display()
{
System.out.println(“Inside class A values : ”+a+” and ”+b);
}
}
class B extends A
{
int c;
void show()
{
System.out.println(“Inside Class B values : ”+a+” , “+b+” and “+c); }
}
class SingleInheritance
{
public static void main(String args[])
{
B obj = new B(); //derived class object
obj.a=10;
obj.b=20;
obj.c=30;
obj.display();
obj.show();
}
}
Output:
Inside class A values : 10 and 20
Inside Class B values : 10 , 20 and 30
When a class is inherited form another derive class then this type fo inheritance is called multilevel inheritance. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.
// Java program to illustrate the
// concept of Multilevel inheritance
class A {
public void message1()
{
System.out.println("Class A");
}
}
class B extends A {
public void message2()
{
System.out.println("Class B");
}
}
class C extends B {
public void message3()
{
System.out.println("Class C");
}
}
public class MultilevelInheritance {
public static void main(String[] args)
{
C obj = new C();
obj.message1();
obj.message2();
obj.message3();
}
}
Output:
Class A
Class B
Class C
When more then one class is derive from a single base class this tyep of inheritance is called hierarchical inheritance. In the below image, class A serves as a base class for the derived class B and C.
// Java program to illustrate the
// concept of Hierarchical inheritance
class A {
public void message1()
{
System.out.println("Class A");
}
}
class B extends A {
public void message2()
{
System.out.println("Class B");
}
}
class C extends A {
public void message3()
{
System.out.println("Class C");
}
}
public class hierarchicalInheritance {
public static void main(String[] args)
{
B obj = new B();
obj.message1();
obj.message2();
C obj2 = new C();
obj2.message1();
obj2.message3();
}
}
Output:
Class A
Class B
Class A
Class C