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
【知识点】
(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
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树