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 Chicken as subclass of Bird and override the sing() method to print "Chicken sings" Design a concrete class named Superman as subclass of Flyable and override the fly() method to print "Superman flies" Design a concrete class named Parrot as subclass of Flyable and Bird and override the methods fly() and sing() to print "Parrot flies" and "Parrot sings" separately.
时间: 2023-12-03 11:46:47 浏览: 399
Here's the implementation of the classes and interface:
```
// Flyable interface
interface Flyable {
void fly();
}
// Bird abstract class
abstract class Bird {
public void sing() {
System.out.println("This bird sings");
}
}
// Chicken concrete class
class Chicken extends Bird {
@Override
public void sing() {
System.out.println("Chicken sings");
}
}
// Superman concrete class
class Superman implements Flyable {
@Override
public void fly() {
System.out.println("Superman flies");
}
}
// Parrot concrete class
class Parrot extends Bird implements Flyable {
@Override
public void fly() {
System.out.println("Parrot flies");
}
@Override
public void sing() {
System.out.println("Parrot sings");
}
}
```
You can now create objects of these classes and call their methods as follows:
```
Bird bird = new Chicken();
bird.sing(); // Output: "Chicken sings"
Flyable flyingObject = new Superman();
flyingObject.fly(); // Output: "Superman flies"
Parrot parrot = new Parrot();
parrot.sing(); // Output: "Parrot sings"
parrot.fly(); // Output: "Parrot flies"
```
阅读全文