Inheritance Program in Java

When a child class object gets all the properties of a parent class object that is called inheritance. In this article, we will learn how to write different inheritance programs in java. We will also learn about various types of inheritance in java with help of examples.

What is Inheritance?

Inheritance is one of the important features of Object Oriented Programming (OOP). This feature helps to derive all the properties of the parent class into the child class. Inheritance is help to create new classes upon existing classes.

Why to use inheritance?

  • Code Reusability:- We can use parent class properties in the child class so we don’t need to write that property again in the child class.
  • Method Overriding:- We can achieve method overriding (Runtime Polymorphism) using inheritance.

Important terms used in inheritance:

  • Parent Class: A parent class is a class from which another class (Child Class) acquires all the properties of the parent class. It is also called a Super class or Base class.

  • Child Class: A child class is a class that acquires all the properties from its parent class. It is also called as Sub class or extended class.

Syntax of inheritance is in java.

Class Parent_class
{
    // parent class methods and fields
}
Class Child_class extends Parent_class
{
    // child class methods and fields
}

In the above syntax, we created a parent class and a child class. We have inherited the parent class in the child class using extends keyword which means the child class derives all the properties of the parent class.

Let’s understand inheritance in java with an example.

In the above image, we can see that we have one parent class named College with a property name and a child class named Branch with property total seats. Let’s write an inheritance program in java using the above example.

class College{
    String name="PrepBytes";
}
class Branch extends College{
    int total_seats=100;
    public static void main(String[] args) {
        Branch computer_science=new Branch();
        System.out.println("College name: "+computer_science.name);
        System.out.println("Total seats: "+computer_science.total_seats);
    }
}
Output:
College name: PrepBytes
Total seats: 100

In the above program, we have created the parent class College and child class Branch which extends the College class. In the output, we can see that we can use parent class variables as well as child class variables in the child class Branch.

Types of inheritance in java:

There are mainly five types of inheritance:

  • Single
  • Multilevel
  • Hierarchical
  • Multiple
  • Hybrid

Out of these five java supports only three inheritance (Single, Multilevel, Multiple). We will see all three types of inheritance program in java.

Single Inheritance Program in java:

When only a single child class extends the single parent class it is called Single inheritance. In the above image of single inheritance, “class2” is a child class that extends the parent class “class1”. Let’s see how to write a single inheritance program in java.

class Vehicle{
    public void ride(){
        System.out.println("riding vehicle");
    }
}
class Type extends Vehicle{
    public void car(){
        System.out.println("vehicle type is car");
    }
}
class PrepBytes{
    public static void main(String[] args) {
        Type type=new Type();
        type.ride();
        type.car();
    }
}
Output:
riding vehicle
vehicle type is car

In the above program, we have created a parent class Vehicle and a child class Type. We can see that the child class Type extends the parent class Vehicle so when we create an object of the child class Type in class PrepBytes we do not need to create another object to fetch the method of the parent class Vehicle. We can use all the properties of the parent class using the child class objects. In the output, we can see that we called both methods using a single child class.

Multilevel Inheritance program in java:

When a sub-child class extends the child class and the child class extends the parent class it is called Multilevel Inheritance. In the above image of multilevel inheritance, “class3” is a sub-child class that extends the child class “class2” and that child class “class2” extends the parent class “class1”. Let’s see how to write a multilevel inheritance program in java.

class Vehicle{
    public void ride(){
        System.out.println("riding vehicle");
    }
}
class Type extends Vehicle{
    public void car(){
        System.out.println("vehicle type is car");
    }
}
class BMW extends Type{
    public void speed(){
        System.out.println("testing speed of BMW");
    }
}
class PrepBytes{
    public static void main(String[] args) {
        BMW bmw=new BMW();
        bmw.ride();
        bmw.car();
        bmw.speed();
    }
}
Output:
riding vehicle
vehicle type is car
testing speed of BMW

In the above program, we created a parent class Vehicle, a child class Type, and a sub-child class BMW. We can see that a sub-child class BMW extends a child class Type and that child class extends parent class Vehicle. Now when we create an object of a sub-child class BMW we don’t need to write methods of parent class in child class. In the output, we can see that we can access all the methods of parent class Vehicle and child class Type in the main method of class PrepBytes.

Hierarchical Inheritance program in java:

When two child class extends the same single parent class it is called Hierarchical Inheritance. In the above image of Hierarchical inheritance, “class2” and “class3” are the two child class which extends the same parent class “class1”. Let’s see how to write a hierarchical inheritance program in java.

class Vehicle{
    public void ride(){
        System.out.println("riding a vehicle");
    }
}
class Car extends Vehicle{
    public void test(){
        System.out.println("Testing a car");
    }
}
class Bike extends Vehicle{
    public void test(){
        System.out.println("Testing a bike");
    }
}

class PrepBytes{
    public static void main(String[] args) {
        Car car=new Car();
        car.ride();
        car.test();
        
        Bike bike=new Bike();
        bike.ride();
        bike.test();
    }
}
Output:
riding a vehicle
Testing a car
riding a vehicle
Testing a bike

In the above program, we created two child classes Car and Bike and we also created a parent class Vehicle. We can see that both the child class Car and Bike extend the same parent class Vehicle. Now when we create an object of the child class Car or Bike we can use the method of parent class in both the child class. In the output, we can see that using the object of both the child class we can access the parent class method in both the child class.

Why Multiple and Hybrid Inheritance program in java is not supported?

Multiple and Hybrid inheritance program in java is not possible to reduce the complexity and make the language more simple. Let’s understand why this type of inheritance program in java is not supported using one example.

In the above image, we can see that we have two parent classes “Class A” and “Class B” and both have the same method test now if we extend both the parent class in the same child class then the question arises that which test method to call in child class. Let’s understand further why this inheritance program in java is not possible.

class Car{
    public void test(){
        System.out.println("Testing a car");
    }
}
class Bike{
    public void test(){
        System.out.println("Testing a bike");
    }
}

class PrepBytes extends Car,Bike{
    public static void main(String[] args) {
        PrepBytes pb= new PrepBytes();
        pb.test();
    }
}

In the above program, we have created two parent classes Car and Bike and both have the same method test. When we extend both the classes in a single child class Prepbytes problem rise when we create an object of the child class PrepBytes and we call the method test compiler to get confused to call the test method of which parent class.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *