Re: question about casting and inheritance
On 1/9/2014 10:48 AM, marc.at.compass@gmail.com wrote:
Hello,
I'm preparing for Java 7 OCA exam. I have a q about casting..
I learned you can cast a variable of a class to another object only if there's a hierarchical relation between the variable's class _and_ the object's class you cast to. And also, the varibla should point to an object og the class you're casting to or you get a ClassCastException during runtime.
So I made up this example:
class A {
String uniqueMethodForA(){
return ("uniqueMethodForA");
}
}
class B extends A {
String uniqueMethodForB(){
return ("uniqueMethodForB");
}
}
public class LearnJava{
public static void main(String[] args) throws Exception {
LearnJava LJ = new LearnJava();
A a = new A();
B b = new B();
System.out.println(a.uniqueMethodForA());
System.out.println(b.uniqueMethodForB());
a=b;
b=(B)a;
System.out.println("=============");
System.out.println(a.uniqueMethodForA());
System.out.println(b.uniqueMethodForB());
}
}
outputs:
uniqueMethodForA
uniqueMethodForB
=============
uniqueMethodForA
uniqueMethodForB
As you see variable a points to an instance of A and not to (an instance of) B. I effectively say "treat a as if it is b". I can even call a method on b that is unique for B. But a didn't point to an instance of B in the first place at all!
Why is it the line b=(B)a; doesn't give me a ClassCastException during runtime?
There are two ways of thinking about Java that, in combination, make
this type of issue very simple and clear:
1. Distinguish between the type of an expression and the class of an object.
2. Every non-null reference expression is a pointer to an object whose
class is matches the expression's type, or extends it, or implements it.
Every expression has a type that is determined at compile time. For a
variable or parameter the type is directly declared. The type of
variable a is A, and the type of variable b is B. Other expressions have
types that can be determined from the nature of the expression and the
types of its operands. The type of "(B)a" is B.
Each object has a class that is determined by how the object is created,
through clone() or new. The class of the object created by "new B()" is
B, and remains B regardless of what variables or expressions point to it.
Applying this thinking to the code above, "a=b" assigns to variable a
the value of b, which is a pointer to an object of class B. Because B
extends A, the compiler knows that variable b either is null or is a
pointer to an object that variable a is allowed to point to, so no cast
is needed.
"b=a" would be rejected at compile time because variable a can point to
objects that variable b cannot point to.
"b=(B)a" is accepted by the compiler because variable a can point to an
object that variable b can also point to. It would cause a
ClassCastException at run time if, in fact, variable a pointed to an
object variable b cannot. It points to an object that was created by
"new B()", an object whose class is B, so the cast is valid.
Patricia