Inheritance in Java with example programs

Inheritance:

In Java, here classes can be reused in several ways, this is done by creating new classes, reusing the properties of the existing class. This mechanism of deriving a new class from an old one that is called Inheritance. The inheritance allows sub-classes to inherit all the variables and methods of their parent classes.

Types of Inheritance in Java:

There are four types of inheritance are exists:

Single Inheritance:

In Single Inheritance, having only one superclass and one subclass. The subclass inherits all the properties and behaviours only from a single class.

single inheritance
In Fig: class A serves as a base class for the derived class B.

Program:

class rectangle
{
int length;
int breadth;
rectangle(int p, int q)
{
length=p;
breadth=q;
}
int area()
{
return(length*breadth);
}
}
class cuboid extends rectangle
{
int height;
cuboid(int p, int q, int r)
{
super(p,q)
height=r;
}
int volume()
{
return(length*breadth*height);
}
}
class test
{
public static void main(String args[])
{
cuboid c1=new cuboid(15,10,8);
int area1=rect.area();
int volume1=rect1.volume();
System.out.println("The Area of Rectangle:"+area1);
System.out.println("The Volume of Cuboid:"+volume1);
}
}

Output:
The Area of Rectangle: 150
The Volume of Cuboid: 1200

Hierarchical Inheritance:

In Hierarchical Inheritance, having one superclass and many subclasses. It supports the hierarchical design of a program.

hierarchical inheritance
In fig: class A serves as the base class for the derived class B, class C, class D.

Program:

class P {
int a = 25;
}
class Q extends P {
void display() {
System.out.println("The value of a into Q class is:" + a);
}
}
class R extends P {
void display() {
System.out.println("The value of an into R class is:" + a);
}
public static void main(String args[])
{
Q q1 = new Q();
q1.display();
R r1 = new R();
r1.display();
}
}

Output:
The value of a into Q class is:25
The value of an into R class is:25

Multi-level Inheritance:

Java supports the Multi-level inheritance concept and uses it extensively in building its class library. It allows us to build a chain of classes.

multilevel inheritance
In Fig: 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.

Program:

class Car{
public Car()
{
System.out.println("Car is moving");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Bike extends Car{
public Bike()
{
System.out.println("Bike is moving");
}
public void brand()
{
System.out.println("Brand: KTM");
}
public void speed()
{
System.out.println("Max Speed: 240Kmph");
}
}
class Scooty extends Bike{
public Scooty()
{
System.out.println("Scooty is moving");
}
public void speed()
{
System.out.println("Max Speed: 120Kmph");
}
public static void main(String args[])
{
Scooty sc=new Scooty();
sc.vehicleType();
sc.brand();
sc.speed();
}
}

Output:
Car is moving
Bike is moving
Scooty is moving
Vehicle Type: Car
Brand: KTM
Max Speed: 120 Kmph

Multiple Inheritance

In multiple inheritances, having only one subclass and many superclasses. A subclass inherits from more than one immediate superclass.

Inheritance in Java with example programs
Program:


class A
{
void display()
{
System.out.println("Hello World!");
}
}
class B
{
void display()
{
System.out.println("Welcome to Webeduclick!");
}
}
class C extends A,B
{
public static void main(String args[])
{
C c1 = new C();
c1.display();
}
}

Note: In Java, it doesn’t implement the Multiple Inheritance.

Hybrid Inheritance:

Hybrid inheritance is a combination of single and multiple inheritances.

Hybrid Inheritance
Program:


public class A
{
public void displayA()
{
System.out.println("Class A");
}
}
public class B extends A
{
public void show()
{
System.out.println("Class B");
}
public void displayB()
{
System.out.println("Method of Class B");
}
}
public class C extends A
{
public void show()
{
System.out.println("Class C");
}
public void displayC()
{
System.out.println(" Method of Class C");
}
}
public class D extends B, C
{
public void displayD()
{
System.out.println("Method of Class D");
}
public static void main(String args[])
{
D d1 = new D();
d1.displayD();
d1.show();
}
}