Java: Abstract Bird Class with Eagle and Hawk Subclasses
Java Abstract Class: Exercise-11 with Solution
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:
// Bird.java
// Define an abstract class named Bird
abstract class Bird {
// Declare an abstract method named fly
public abstract void fly();
// Declare an abstract method named makeSound
public abstract void makeSound();
}
// Eagle.java
// Define a class named Eagle that extends Bird
class Eagle extends Bird {
// Override the fly method from Bird
@Override
// Implementation of the fly method that prints a message
public void fly() {
System.out.println("Eagle: Flying high in the sky.");
}
// Override the makeSound method from Bird
@Override
// Implementation of the makeSound method that prints a message
public void makeSound() {
System.out.println("Eagle: Screech! Screech!");
}
}
// Hawk.java
// Define a class named Hawk that extends Bird
class Hawk extends Bird {
// Override the fly method from Bird
@Override
// Implementation of the fly method that prints a message
public void fly() {
System.out.println("Hawk: Soaring through the air.");
}
// Override the makeSound method from Bird
@Override
// Implementation of the makeSound method that prints a message
public void makeSound() {
System.out.println("Hawk: Caw! Caw!");
}
}
// Main.java
// Define a public class named Main
public class Main {
// Define the main method, which is the entry point of the program
public static void main(String[] args) {
// Create an instance of Eagle and assign it to a Bird reference
Bird eagle = new Eagle();
// Create an instance of Hawk and assign it to a Bird reference
Bird hawk = new Hawk();
// Call the fly method on the eagle object
eagle.fly();
// Call the makeSound method on the eagle object
eagle.makeSound();
// Call the fly method on the hawk object
hawk.fly();
// Call the makeSound method on the hawk object
hawk.makeSound();
}
}
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:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: 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?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/java-abstract-class-exercise-11.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics