If you are a subclass of an interface, you have to override all of its method signatures.这句话为什么是错的
时间: 2023-12-06 20:40:37 浏览: 32
这句话是不完全正确的。在Java中,如果一个类实现了一个接口,那么它必须实现该接口中定义的所有方法。但是,如果这个类是一个抽象类,那么它可以选择性地实现接口中的某些方法,而不是全部都实现。此外,如果这个类是另一个接口的子接口,它也可以选择性地实现该接口的某些方法,而不是全部都实现。因此,这句话应该被修改为“如果一个类实现了一个接口,那么它必须实现该接口中定义的所有方法,除非它是一个抽象类或者是另一个接口的子接口”。
相关问题
The method removeComplaint(Complaint) of type ComplaintDaoImpl must override or implement a supertype method
This error message usually occurs when a method in a subclass is intended to override a method in its superclass or interface, but the method signature (i.e., the method name, parameter types, and return type) does not match exactly with the method in the superclass or interface.
In the case of the method removeComplaint(Complaint) of type ComplaintDaoImpl, it means that this method is supposed to override a method from its superclass or interface, but the method signature is not identical to the method it is supposed to override.
To fix this error, you need to ensure that the method signature of the removeComplaint(Complaint) method in the ComplaintDaoImpl class matches exactly with the method it is supposed to override in its superclass or interface. This includes the method name, parameter types, and return type.
For example, if the removeComplaint(Complaint) method is supposed to override a method in an interface called ComplaintDao, the method signature in the ComplaintDaoImpl class should look like this:
public void removeComplaint(Complaint complaint) {
// implementation code here
}
Note that the method name, parameter type (Complaint), and return type (void) match exactly with the method in the ComplaintDao interface.
By ensuring that the method signature matches exactly with the method it is supposed to override, you can eliminate the "method must override or implement a supertype method" error and ensure that your code functions as intended.
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.
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"
```