the type on the left hand side was an A; however this has nothing to do with the cause of the ClassCastException at runtime. Quite simply, a B reference can never point at an object of type A (and that is what the right hand side of the assignment was trying to create).
Given that is the case, the following also holds true:
class A implements I{
void iMsg() { }
void aMethod() { }
}
class B extends A {
void bMethod() { }
}
class C extends B{
void cMethod() { }
}
And the following declarations: A a = new A();
B b = new B();
a = (B)(I)b;
a.bMethod();
Why does a.bMethod cause a compiler error: Test4.java:35: error: cannot find symbol
a.bMethod();
^
symbol: method bMethod()
location: variable a of type A
I've summarized my understanding of this multiple casting question here: http://www.coderanch.com/forums/posts/l ... 31#3066425
I'd appreciate if somebody interested and competent would find the time to verify, if my understanding is correct. Many thanks!
interface I{}
class A implements I{}
class B extends A {}
class C extends B{}
public class TestClass {
public static void main(String[] args){
A a = new A();
B b = new B();
a=b;
//b=(B)(I)a;
b=a; // if "a" is a "b" now, why this line of code is wrong?
}
}
if "a" is a "b" now, why this line of code is wrong?
Your question indicates several misconceptions.
1. It is incorrect to say that "a" is a "b" because "b" is reference variable. You can say "a" points to an object of class B.
2. When you cast a reference of one class to another class, you don't change the actual type of the object that is pointed to by the reference. So when you cast a to B ( i.e. by doing (B) a), you don't convert the object pointed to by the variable a into an object of class B. You merely tell the compiler that a will point to an object of class B at run time.
Thus, at run time, a really has to point to an object of class B for (B) a to succeed at runtime.
I suggest you to read this topic thoroughly from a good book to get your basics right before attempting mock exams.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
This one tripped me up. I'm glad it did because now it's cleared up an obvious misunderstanding that I had.
It seems tricky but actually, if you think about it, it isn't.
Just remember, if A implements I, an A is an I but an I isn't an A.
To reinforce the point, millions of classes implement Serializable; but that doesn't mean that just because X implements Serializable, any Serializable reference can be assigned to a reference variable of type X.
What makes it deceptive is that when you see
a = (I) b;
you know that A is an I, so you think, surely an I can be assigne to variable a. But the compiler just knows that at that point there is something that is an I. It is only known to be an I; it could be any one of millions of classes that is implements I. So you can't just assign it to a variable of type A without explicitly casting it.
public class Test {
public static void main(String[] args) {
I i = null;
Integer s = (Integer) i;
}
}
interface I{}
I went through the discussion cursorily - I doesn't seem to be about the same thing. But if the answer is there, I could read it all and try to understand, however I guess then I'll have questions to the discussion itself.
Ok, I see the issue. The phrase "any class" is not completely correct. It should say "any class except a class that is final and does not implement I". The principle behind it is that a variable of one class can, at runtime, point to an object of a subclass that implements I. Therefore, the compiler has no option but to accept the cast. Of course, it fails for Integer because it is final and the compiler knows that it is not possible for a variable of type Integer to point to an object of a class that implements I.
If you like our products and services, please help us by posting your review here.