About Question enthuware.ocajp.i.v7.2.872 :
Posted: Sun Mar 25, 2012 2:36 pm
Look at this question
class A {
public int getCode(){ return 2;}
}
class AA extends A {
public void doStuff() {
}
public static void main(String[] args) {
A a = null;
AA aa = null;
a =(AA) aa;
a = new AA();
}
}
Given the following two declarations, which of the options will compile?
A a = null;
AA aa = null;
Answers:
a = (AA) aa; Correct - Agreed
a = new AA(); Correct - Agreed
((AA) a).doStuff(); Correct - Agreed
aa = (AA) a; Incorrect - Not agreed
This questiondoes not ask if the code inserted must run without errors, so the option 3 is compilable but not runnable. The option 4 is compilable and not runnable too, and still wrong. Can anyone clarify it to me please?
Thanks.
class A {
public int getCode(){ return 2;}
}
class AA extends A {
public void doStuff() {
}
public static void main(String[] args) {
A a = null;
AA aa = null;
a =(AA) aa;
a = new AA();
}
}
Given the following two declarations, which of the options will compile?
A a = null;
AA aa = null;
Answers:
a = (AA) aa; Correct - Agreed
a = new AA(); Correct - Agreed
((AA) a).doStuff(); Correct - Agreed
aa = (AA) a; Incorrect - Not agreed
This questiondoes not ask if the code inserted must run without errors, so the option 3 is compilable but not runnable. The option 4 is compilable and not runnable too, and still wrong. Can anyone clarify it to me please?
Thanks.