SCJP(OCPJP) 考古題解析 第38題

Given:

class One {
    public One foo() { return this; }
}
class Two extends One {
    public One foo() { return this; }
}
class Three extends Two { 
    // insert method here
}

Which two methods, inserted individually, correctly complete the Three class? (Choose two.)

A. public void foo(){}
B. public int foo(){return 3;}
C. public Two foo(){return this;}
D. public One foo(){return this;}
E. public Object foo(){return this;}


答案:CD



解析:


所謂的Overriding指的是

"子類別定義跟父類別一樣的方法簽章,但提供不同的實作"



所謂一樣的方法簽章指的是方法名稱回傳型別以及參數傳遞列必須一樣

但從Java 5.0 開始

回傳型別可以是被Overriden方法回傳型別的子類別




0 意見:

張貼留言