Abstract Class Extending Another in Java


Write a Java program to demonstrate an abstract class extending another abstract class

  • There are two abstract classes, Animal and Mammal. Animal has an abstract method makeSound(), while Mammal extends Animal and adds another abstract method move().
  • The Dog class extends Mammal and provides implementations for both makeSound() and move() methods.
  • In the Main class, an instance of Dog is created and its makeSound() and move() methods are called, which prints "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