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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值