Given:
interface Doable {
public void doSomething(String s);
}
Which two class definitions compile?
A.
public abstract class Task implements Doable {
public void doSomethingElse(String s) {
}
}
B.
public abstract class Work implements Doable {
public abstract void doSomething(String s) {
}
public void doYourThing(Boolean b) {
}
}
C.
public class Job implements Doable {
public void doSomething(Integer i) {
}
}
D.
public class Action implements Doable {
public void doSomething(Integer i) {
}
public String doThis(Integer j) {
}
}
E.
public class Do implements Doable {
public void doSomething(Integer i) {
}
public void doSomething(String s) {
}
public void doThat(String s) {
}
}
題解
選項 A,雖然 Task 類別沒有實作出 Doable 介面的 doSomething 方法,但是 Task 類別為抽象類別,因此允許未實作的方法。
選項 B,抽象方法不能有程式實作區塊。
選項 C,Job 類別的 doSomething 方法傳入的參數和 Doable 介面的 doSomething 方法不同,將不被認為是實作 Doable 介面的 doSomething 方法,所以 Job 類別會因沒有實作 Doable 介面的 doSomething 方法而無法編譯。
選項 D,錯誤理由同選項 C。
選項 E,有成功實作出 Doable 介面的 doSomething 方法。
