About Question enthuware.ocpjp.v7.2.1497 :
Posted: Fri Jun 14, 2013 10:34 pm
class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest
{
public static void main(String args[])
{
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
// insert statement here
}
}
_______________________
A. b = b1; Not a problem because B1 IS-A B -- I get it.
D. b2 = (B2) b1;
"It will not compile because b1 can never point to an object of class B2."
I thought B2 IS-A B1 and B2 IS-A B. If so, a B1 reference could be assigned to a B2 object, and later transferred to a reference of its own type with the above cast.
Not so?
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest
{
public static void main(String args[])
{
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
// insert statement here
}
}
_______________________
A. b = b1; Not a problem because B1 IS-A B -- I get it.
D. b2 = (B2) b1;
"It will not compile because b1 can never point to an object of class B2."
I thought B2 IS-A B1 and B2 IS-A B. If so, a B1 reference could be assigned to a B2 object, and later transferred to a reference of its own type with the above cast.
Not so?