Write a Java program to Implement multilevel inheritance
This Java program demonstrates multilevel inheritance, which is a concept in object-oriented programming where a class inherits properties and behaviors from a parent class (superclass), and then another class inherits from that derived class. In this program, there are three classes: Animal, Dog, and Puppy, forming a multilevel inheritance hierarchy. Here's an explanation of the program:
- public class MultilevelInheritance: 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 an instance of the Puppy class and demonstrates multilevel inheritance by calling methods from the Animal, Dog, and Puppy classes.
- Puppy puppy = new Puppy("Buddy");: This line creates an object of the Puppy class named "Buddy." The constructor of the Puppy class is called, which, in turn, calls the constructor of the Dog class (its parent class), which in turn calls the constructor of the Animal class (its grandparent class).
- puppy.eat();: This line calls the eat method of the Puppy object, which is inherited from the Animal class. The output will be "Buddy is Eating."
- puppy.bark();: This line calls the bark method of the Puppy object, which is inherited from the Dog class. The output will be "Buddy is Barking."
- puppy.play();: This line calls the play method of the Puppy object, which is specific to the Puppy class. The output will be "Buddy is Playing."
- class Animal: This is the grandparent class (superclass) that represents generic animal properties and behaviors. It has an instance variable name and a parameterized constructor that initializes the name.
- public void eat(): The eat method of the Animal class simply prints a message indicating that the animal is eating, including its name.
- class Dog extends Animal: This is the parent class (subclass) that inherits from the Animal class. It represents a specific type of animal, a dog. The Dog class has its own method, bark, which is not present in the Animal class.
- public Dog(String name): The constructor of the Dog class takes a name as a parameter and calls the constructor of the Animal class (its grandparent class) using super(name) to initialize the name property.
- public void bark(): The bark method of the Dog class prints a message indicating that the dog is barking, including its name.
- class Puppy extends Dog: This is the child class (subclass) that inherits from the Dog class. It represents a specific type of dog, a puppy. The Puppy class has its own method, play, which is not present in the Animal or Dog classes.
- public Puppy(String name): The constructor of the Puppy class takes a name as a parameter and calls the constructor of the Dog class (its parent class) using super(name) to initialize the name property.
- public void play(): The play method of the Puppy class prints a message indicating that the puppy is playing, including its name.
This program demonstrates multilevel inheritance, where the Puppy class inherits properties and methods from the Dog class, which, in turn, inherits from the Animal class. When an instance of the Puppy class is created, it can access the behavior of all three classes in the hierarchy.
Source Code
public class MultilevelInheritance // Main class
{
public static void main(String[] args)
{
Puppy puppy = new Puppy("Buddy"); // Create an instance of Puppy
puppy.eat(); // Call methods from Animal class
puppy.bark(); // Call methods from Dog class
puppy.play(); // Call methods from Puppy class
}
}
class Animal // Grandparent class
{
protected String name;
public Animal(String name)
{
this.name = name;
}
public void eat()
{
System.out.println(name + " is Eating");
}
}
class Dog extends Animal // Parent class inheriting from Animal
{
public Dog(String name)
{
super(name);
}
public void bark()
{
System.out.println(name + " is Barking");
}
}
class Puppy extends Dog // Child class inheriting from Dog
{
public Puppy(String name)
{
super(name);
}
public void play()
{
System.out.println(name + " is Playing");
}
}
Output
Buddy is Eating
Buddy is Barking
Buddy is Playing