Given:



class Mid {

    public int findMid(int n1, int n2) {
        return (n1 + n2) / 2;
    }
}

public class Calc extends Mid {

    public static void main(String[] args) {
        int n1 = 22, n2 = 2;
        // insert code here
        System.out.print(n3);
    }
}

Which two code fragments, when inserted at // insert code here, enable the code to compile and print 12?

A.

Calc c = new Calc();
int n3 = c.findMid(n1, n2);

B.

int n3 = super.findMid(n1, n3);

C.

Calc c = new Mid();
int n3 = c.findMid(n1, n2);

D.

Mid m1 = new Calc();
int n3 = m1.findMid(n1, n2);

E.

int n3 = Calc.findMid(n1, n2);

題解

findMid是物件方法,必須要實體化出Calc或是Mid的物件才能使用,因此選項B、E是錯的。

Calc類別繼承Mid類別,所以Calc物件型態可以隱含式(implicit)轉型為Mid物件型態,反之則不行,因此選項C是錯誤的。

最後剩下的選項A、D是正確的。