Given:



public abstract class Shape {

    private int x;
    private int y;

    public abstract void draw();

    public void setAnchor(int x, int y) {
        this.x = x;
        this.x = y;
    }
}

Which two classes use the shape class correctly?

A.

public class Circle implements Shape{
    private int radius;
}

B.

public abstract class Circle extends Shape{
    private int radius;
}

C.

public class Circle extends Shape{
    private int radius;
    public void draw();
}

D.

public abstract class Circle implements Shape{
    private int radius;
    public void draw();
}

E.

public class Circle extends Shape{
    private int radius;
    public void draw() { /* code here */ }
}

F.

public abstract class Circle implements Shape{
    private int radius;
    public void draw() { /* code here */ }
}

題解

這題是在考抽象類別(abstract class)的用法。

抽象類別內允許沒有實作的抽象方法。由於抽象類別也是類別,因此無法使用實作介面(interface)的「implements」關鍵字來實作,所以選項A、D、F都是錯誤的。另外,抽象類別的抽象方法必須被繼承該抽象類別的非抽象類別來實作,因此選項C也是錯誤的。