Write a Java program to Implement hierarchical inheritance


This Java program demonstrates hierarchical inheritance, a concept in object-oriented programming where multiple child classes inherit properties and behaviors from a common parent class. In this program, there are three classes: Vehicle, Car, and Motorcycle, forming a hierarchical inheritance structure. Here's an explanation of the program:

  • public class HierarchicalInheritance: This is the main class that contains the main method, the entry point of the program.
  • public static void main(String[] args): The main method is the starting point of the program. It creates objects of the Car and Motorcycle classes and demonstrates hierarchical inheritance by calling methods from both the parent class (Vehicle) and the child classes (Car and Motorcycle).
  • Car car = new Car(); and Motorcycle motorcycle = new Motorcycle();: These lines create objects of the Car and Motorcycle classes.
  • car.display(); and motorcycle.display();: These lines call the display method of the Vehicle class, which is inherited by both the Car and Motorcycle classes. The output will be "This is a Vehicle" for both objects, as both child classes inherit this method from the parent class.
  • car.displayCar(); and motorcycle.displayMotorcycle();: These lines call methods specific to the child classes. The Car object calls the displayCar method, and the Motorcycle object calls the displayMotorcycle method. These methods are unique to their respective child classes and are not inherited by other classes. The output will be "This is a Car" for the Car object and "This is a Motorcycle" for the Motorcycle object.
  • class Vehicle: This is the parent class (superclass) that represents generic vehicle properties and behaviors.
  • void display(): The display method of the Vehicle class simply prints a message indicating that it is a vehicle.
  • class Car extends Vehicle: This is one of the child classes (subclasses) that inherits from the Vehicle class. It represents a specific type of vehicle, a car.
  • void displayCar(): The displayCar method of the Car class prints a message indicating that it is a car.
  • class Motorcycle extends Vehicle: This is another child class (subclass) that also inherits from the Vehicle class. It represents a specific type of vehicle, a motorcycle.
  • void displayMotorcycle(): The displayMotorcycle method of the Motorcycle class prints a message indicating that it is a motorcycle.

This program demonstrates hierarchical inheritance, where both the Car and Motorcycle classes inherit properties and methods from the common parent class, Vehicle. Additionally, each child class has its own unique methods to represent specific behavior for cars and motorcycles.


Source Code

public class HierarchicalInheritance	// Main class
{
	public static void main(String[] args)
	{        
		Car car = new Car();	// Create objects of child classes
		Motorcycle motorcycle = new Motorcycle();
 
		car.display();	// Calling methods of parent class
		motorcycle.display();
 
		car.displayCar();	// Calling methods of child classes
		motorcycle.displayMotorcycle();
	}
}
class Vehicle	// Parent class
{
	void display()
	{
		System.out.println("This is a Vehicle");
	}
}
class Car extends Vehicle	// Child class 1
{
	void displayCar()
	{
		System.out.println("This is a Car");
	}
}
class Motorcycle extends Vehicle	// Child class 2
{
	void displayMotorcycle()
	{
		System.out.println("This is a Motorcycle");
	}
}

Output

This is a Vehicle
This is a Vehicle
This is a Car
This is a Motorcycle