Java Polymorphism Programming - Animal Class with Bird and Cat Subclasses for Specific Sounds
Write a Java program to create a base class Animal (Animal Family) with a method called Sound(). Create two subclasses Bird and Cat. Override the Sound() method in each subclass to make a specific sound for each animal.
In the given exercise, here is a simple diagram illustrating polymorphism implementation:

In the above diagram, we have a base class Animal with the makeSound() method. It serves as the superclass for two subclasses, Bird and Cat. Both subclasses override the makeSound() method to provide their own sound implementations.
Sample Solution:
Java Code:
Output:
The animal makes a sound The bird chirps The cat meows
Flowchart:




For more Practice: Solve these Related Problems:
- Write a Java program where the "Animal" class includes a move() method describing how each animal moves.
- Write a Java program where the "Animal" class supports a lifespan attribute with different values for each subclass.
- Write a Java program where the "Animal" class includes a method to check if the animal is domesticated.
- Write a Java program where the "Animal" class implements a feeding behavior method.
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Java Polymorphism Exercises Home.
Next: Vehicle Class with Car and Bicycle Subclasses for Speed Control.
What is the difficulty level of this exercise?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics