Given:
What is the result?
A.
Base
DerivedA
B.
Base
DerivedB
C.
DerivedB
DerivedB
D.
DerivedB
DerivedA
E.
A classcast Exception is thrown at runtime.
Given:
What is the result?
A.
Base
DerivedA
B.
Base
DerivedB
C.
DerivedB
DerivedB
D.
DerivedB
DerivedA
E.
A classcast Exception is thrown at runtime.
Answer: C
package q075;
class Base {
public void test() {
System.out.println(“Base”);
}
}
package q075;
class DerivedA extends Base {
public void test() {
System.out.println(“DerivedA “);
}
}
package q075;
public class DerivedB extends DerivedA{
public void test() {
System.out.println(“DerivedB “);
}
public static void main(String[] args) {
Base b1 = new DerivedB();
Base b2 = new DerivedA();
Base b3 = new DerivedB();
b1 = (Base) b3;
Base b4 = (DerivedA) b3;
b1.test();
b4.test();
}
}
/* Output:
DerivedB
DerivedB
*/
C
C