Java: Abstract Bird Class with Eagle and Hawk Subclasses
Write a Java program to create an abstract class Bird with abstract methods fly() and makeSound(). Create subclasses Eagle and Hawk that extend the Bird class and implement the respective methods to describe how each bird flies and makes a sound.
Sample Solution:
Java Code:
Output:
Eagle: Flying high in the sky. Eagle: Screech! Screech! Hawk: Soaring through the air. Hawk: Caw! Caw!
Explanation:
In the above exercise -
- The abstract class "Bird" has two abstract methods fly() and makeSound(). The subclasses Eagle and Hawk extend the Bird class and provide their own implementations for these abstract methods.
- The "Eagle" class describes how an eagle flies high in the sky and makes a screeching sound, while the "Hawk" class describes how a hawk soars through the air and makes a cawing sound.
- In the main method, we create instances of Eagle and Hawk, and then call the fly() and makeSound() methods on each object to describe how each bird flies and makes a sound.
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Java program where the "Eagle" subclass includes a method to track flight speed.
- Write a Java program where the "Hawk" subclass implements a method to detect prey from a distance.
- Write a Java program where the "Bird" class includes an attribute for wingspan, and subclasses override it.
- Write a Java program where the "Eagle" subclass adds a method to perform aerial maneuvers.
Go to:
Java Code Editor:
Contribute your code and comments through Disqus.
PREV : Abstract Shape2D Class with Rectangle and Circle Subclasses.
NEXT : Abstract GeometricShape Class with Triangle and Square Subclasses.
What is the difficulty level of this exercise?