Design an abstract class named Bird to represent a bird. The class contains an void sing() method to print "This bird sings". Design an interface named Flyable to represent anything can fly and contains an void fly() method. Design a concrete class named
时间: 2024-02-01 11:02:18 浏览: 74
A MATLAB class to represent the tree data structure..zip
Sparrow that extends the Bird class and implements the Flyable interface. The Sparrow class should have an overridden sing() method to print "The sparrow sings" and an overridden fly() method to print "The sparrow flies".
Here is the code implementation:
```
public abstract class Bird {
public void sing() {
System.out.println("This bird sings");
}
}
public interface Flyable {
public void fly();
}
public class Sparrow extends Bird implements Flyable {
@Override
public void sing() {
System.out.println("The sparrow sings");
}
@Override
public void fly() {
System.out.println("The sparrow flies");
}
}
```
Now, we can create a Sparrow object and call its methods:
```
Sparrow sparrow = new Sparrow();
sparrow.sing(); // Output: The sparrow sings
sparrow.fly(); // Output: The sparrow flies
```
阅读全文