Multiple Inheritance Levels in Java


Create a Java program to demonstrate multiple levels of inheritance with abstract classes

  • The Animal class is declared as abstract with an abstract method makeSound().
  • The Mammal class extends Animal and is also declared as abstract with an abstract method move().
  • The Dog class extends Mammal and provides concrete implementations for both makeSound() and move() methods.
  • In the main method, an instance of Dog is created, and its makeSound() and move() methods are called, resulting in the output of "Dog barks" and "Dog runs" respectively.

Source Code

abstract class Animal
{
	abstract void makeSound();
}
 
abstract class Mammal extends Animal
{
	abstract void move();
}
 
class Dog extends Mammal
{
	@Override
	void makeSound()
	{
		System.out.println("Dog barks");
	}
 
	@Override
	void move()
	{
		System.out.println("Dog runs");
	}
}
 
public class Main
{
	public static void main(String[] args)
	{
		Dog dog = new Dog();
		dog.makeSound();
		dog.move();
	}
}

Output

Dog barks
Dog runs