Part I: Assignments and Casting (1 point)
------------------------------------------
(a) At compile-time, can we assign xa to ya, and vice versa? When is a cast
required?
Ans: ya = xa 不可以,但是反过来 xa = ya 是可以的。
ya = (Y[]) xa 这样也是可以的
(b) At run-time, if ya references an array of Y's, can we assign it to xa?
Can we then assign it back from xa to ya?
Ans: 可以
(c) If xa references an array of X's (that are not Y's), can we assign it to
ya? Can we then assign it back from ya to xa? Does it make a difference
if the array of type X[] references objects that are all of class Y? Why
do you think this is the case?
Ans: 不可以 如果the array of type X[] references objects that are all of class Y的话,用cast就可以了
Part II: Conflicting Declarations (1 point)
--------------------------------------------
(a) Will Java compile the result?
Ans: 可以
(b) What if the method declaration in the interface has a different return
type?
Ans: 不可以,两个方法会冲突
(c) What if the method declaration in the interface has the same return type,
but a signature with a different parameter type?
Ans: 不可以,这样的话在subclass需要将interface里的方法实现了
(d) What if the method declaration in the interface has the same return type,
and the same number of parameters and parameter types, but those
parameters have different names?
Ans: 可以
Part III: More Conflicting Declarations (1 point)
--------------------------------------------------
(a) Will Java compile the result? Does it make any difference whether the
constant in the superclass and the constant in the interface have the
same value?
Ans: 可以,是不是同一个值无所谓
(b) Write a main() method in the subclass that accesses the constant using the
same name used in the superclass and the Java interface. Will Java
compile the result? Does it make any difference whether the constant in
the superclass and the constant in the interface have the same value?
Ans: 不可以
(c) Figure out how to modify your main() method so that it accesses and prints
one of the two conflicting values. (Look to the Lecture 9 notes for
clues.) Make sure your code compiles and runs without errors.
Part IV: Method Overriding (1 point)
-------------------------------------
(a) Define a variable whose static type is the subclass and which references
an object of the subclass. If we cast the variable to the superclass type
before calling the overridden method
((Superclass) subclassvariable).method();
does Java call the superclass method or the subclass method?
Ans: 调用的是the subclass method
(b) Define a variable whose static type is the superclass and which references
an object of the superclass (but not the subclass). If we cast the
variable to the subclass type before calling the method, does Java call
the superclass method or the subclass method?
Ans: run-time error 不能cast to the subclass type
(c) Suppose you have an object whose class is the subclass. Can you figure
out a way to call the superclass method on that object without having to
go through the subclass method of the same name?
Part I:
a.ya = xa; // error
xa = ya; // OK
ya = (Y[])xa;// OK
b.xa = ya;// OK
back fail
c.fail; still fail. JAVA compiles code 1 line at a time,不知道xa指向的是Y
Part II:
a.OK
b.error
c.error
d.OK
Part III:
a.OK 不影响
b.都可以输出
c.此处假设 public class Y extends X implements Z
在X,Z中均设置x0的值,X中go(int num)返回x0,
System.out.print(Z.x0);
System.out.println(x.go(1));
Part IV:
a.subclass method
b.fail ?? 无法实现,throw exception
c.在Y中写一个method:
int useSuper(){
return super.go(3);
}
然后利用y调用useSuper()
补充内容 (2016-4-8 18:21):
更正:
part2 b.back ya = (Y[])xa; //OK
c.run-time error ;if the array of type X[] references objects that are all of class Y, 可行
Part3:b.compile-time error
orz...
最后一个问题我也没读懂,我的理解是:
(c) Suppose you have an object whose class is the subclass. Can you figure
out a way to call the superclass method on that object without having to
go through the subclass method of the same name?
*********************************************
I'm not sure whether I got the meaning, but I can. If a method in superclass is static
and overriden in subclass, let me suppose that I have a subclass object, which method
will be invoked depends on the reference variable. If a superclass variable references
the subclass object and invoke the static method, the superclass static method will be
invoked, whereas if a subclass variable references the subclass object, the subclass
static method will be invoked.
I'm not sure the whether it's correct to define "override a static method", and if so,
could I explian that "there isn't polimorphism in static method"?