您的位置:首页 > 编程语言 > Java开发

Java认证考试实例疑难辨析(9)

2016-02-15 15:22 465 查看
9
【知识点】
(1)多类型继承(Multiple Inheritance of Type)
Java语言中,某个类在继承超类或实现若干接口后,这个类也就具有了超类或接口的类型,相当于是它们的子类。
(2)方法覆写(Overriding)
如果子类中的实例方法与超类中的实例方法具有相同的方法签名,即方法名称、方法参数的类型和个数、方法返回类型都相同,则称为子类覆写了超类的方法。
子类覆写方法的返回类型也可以是超类中被覆写方法返回类型的子类型。
注:方法覆写也经常称为重写。
【例题】
Which statement is true about the classes andinterfaces in the exhibit?
01. public interface A {
02.   publicvoid doSomething(String thing);
03. }
01. public class AImpl implements A {
02.   publicvoid doSomething(String msg) {}
03. }
01. public class B {
02.   public Adoit(){
03.     //more code here
04.   }
05.   public String execute(){
06     //more code here
07   }
08. }
01. public class C extends B {
02.   publicAImpl doit(){
03.     //more code here
04.   }
05.
06.   public Object execute() {
07.     //morecode here
08.   }
09. }
A. Compilation will succeed for all classes andinterfaces.
B. Compilation of class C will fail because of anerror in line 2.
C. Compilation of class C will fail because of anerror in line 6.
D. Compilation of class AImpl will fail because of anerror in line 2.
【Answer】 C
【例题辨析】
(1)AImpl类实现了接口A,因此AImpl类相当于是A的子类;
(2)C类继承B类时,覆写了方法doit(),根据方法覆写规则,方法名、参数个数和类型必须相同,方法返回类型必须相同或是被覆写方法的子类,C类的doit()方法返回类型AImpl是B类中doit()返回类型A的子类,因此,符合方法覆写规则。
(3)C类继承B类时,还覆写了方法execute(),根据方法覆写规则,方法名、参数个数和类型必须相同,方法返回类型必须相同或是被覆写方法的子类,C类的execute ()方法返回类型Object,而B类中execute ()返回类型是String,显然类型不兼容,不符合方法覆写规则,编译失败。
综上所述,可以排除ABD
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息