abstract interface A extends I1,I2 {abstract void print() {};}
时间: 2023-09-03 09:13:16 浏览: 73
Java中不允许在接口中使用 `abstract` 修饰符,因为接口中的所有方法默认都是抽象方法,不需要再使用 `abstract` 关键字进行修饰。因此,该代码是错误的。另外,接口中的方法不能有方法体,因此也不能像上面的代码一样为方法提供具体的实现。如果需要提供默认的方法实现,可以在Java 8及以上版本中使用 `default` 关键字。
相关问题
40、以下哪个接口的定义是正确的?( D ) A、 interface B { void print() { } ;} B、 abstract interface B { void print() ;} C、 abstract interface B extends A1,A2 //A1、A2为已定义的接口 { abstract void print(){ };} D、 interface B { void print();}
选项D的接口定义是正确的。在Java中,接口是一种特殊的抽象类,它定义了一组方法的签名,但没有实现。接口中的方法都是抽象的,不能有实现代码。接口的定义方式是使用关键字interface,后面跟着接口名称和方法签名。例如,选项D中的接口定义了一个名为B的接口,该接口有一个名为print的方法,没有方法体。选项A中的接口定义了一个名为B的抽象类,该类有一个名为print的方法,并且有方法体,这是不合法的。选项B中的接口定义了一个名为B的抽象类,该类有一个名为print的抽象方法,没有方法体,但是关键字abstract在接口定义中是多余的。选项C中的接口定义了一个名为B的抽象接口,该接口继承了A1和A2两个接口,并且有一个名为print的抽象方法,但是方法体中的分号是多余的。
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 contai
ns a void fly() method. The Bird class should implement the Flyable interface and override the fly() method to print This bird flies. Additionally, the Bird class should have a protected string field named color to represent the color of the bird. The class should have a constructor that takes a string parameter to set the color of the bird. Finally, the Bird class should have a public getColor() method that returns the color of the bird. Here's an example implementation in Java:
```
public interface Flyable {
void fly();
}
public abstract class Bird implements Flyable {
protected String color;
public Bird(String color) {
this.color = color;
}
public String getColor() {
return color;
}
@Override
public void fly() {
System.out.println("This bird flies.");
}
public abstract void sing();
}
public class Sparrow extends Bird {
public Sparrow(String color) {
super(color);
}
@Override
public void sing() {
System.out.println("This bird sings.");
}
}
```