Page 2 of 3

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Fri Dec 13, 2013 9:43 pm
by admin
>>WHY does a.m1() not give me the output "B.m1()"? if a points to instance of B it should?
Because a does not point to an instance of B. It points to an instance of C. That what is polymorphism. http://docs.oracle.com/javase/tutorial/ ... phism.html

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Mon Jan 20, 2014 6:41 pm
by tn1408
Hello, Paul I ran the code:
class TestClass{
public static void main(String[] args){
A a = new A();
B b = new B();

a = (B)(I)b;
b = (B)(I) a;
//I i = (C) a;
//I i = (C) a;
}
}
interface I{}
class A implements I{}
class B extends A {}
class C extends B{}

and answer #2: b = (B)(I) a; actually didn't fail.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Mon Jan 20, 2014 10:28 pm
by admin
That is because you already have "a = (B)(I)b;". You need to comment it out first.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Thu Feb 06, 2014 2:21 pm
by seanthemill
Hi,
Given the following code:

Code: Select all

class A{}
class B extends A {}

public class Test {
    public static void main(String[] args) {
        A a = new A();
        A a1=new A();
        B b = new B();
//        a=b;// ok
//        b=(B)a;// ClassCastException
//        a=(A)a1;  // ok
//        a=a1;  // ok
        
      a=(B)a1; // compiles ok, ClassCastException???
    }
}
My question is with the line in bold/red. My understanding is that for the compiler to be ok, it just needs to be satisfied that the classes are in the same hierarchy and as a result it may work (up the tree implicit casting, down the tree requires explicit casting). Whenever I have come across ClassCastException it is because the reference was pointing to an object up the tree e.g. a ref of type B pointing to an object of type A.
The line in question appears to be a ref of type A pointing to an object of type A. The cast to (B) obviously is what is causing the ClassCastException. Can someone explain please what it does to effect this? Is the A ref a, a B ref all of a sudden??

Thanks,
Sean.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Thu Feb 06, 2014 9:33 pm
by admin
You've written about your understanding about compiler. But then you are talking about ClassCastException, which happens only at run time.
I am not really clear about what you are asking? ClassCastException is thrown whenever the object that you are trying to cast to a reference of another class cannot be cast to that class.

I don't think in your code b=(B)a should throw ClassCastException because a is actually pointing to an object of class B because of a=b; written earlier.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Fri Feb 07, 2014 12:10 pm
by seanthemill
Thanks for the reply. I think I have it figured: my confusion was arising because of the fact that given the assignment

Code: Select all

 A a = (B)new A(); // where B extends from A
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:

Code: Select all

A a = (B)new B();   // no issues at compile time or runtime
because a reference of type A can point at any object of type B (because B is-an A via inheritance).

Is that accurate?

Regards,
Seán.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Fri Feb 07, 2014 9:45 pm
by admin
Yes, that is correct.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sat Dec 26, 2015 12:30 pm
by iamslrr
Consider the following classes :

interface I{
void iMsg();
}

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


After the cast a instanceof B is true.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sat Dec 26, 2015 12:34 pm
by iamslrr
Ahhh...
Nevermind!

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Fri Jan 15, 2016 4:53 am
by AristPhil
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!

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Fri Oct 20, 2017 5:25 pm
by Sergey

Code: Select all

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?

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sat Oct 21, 2017 11:49 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sat Jun 30, 2018 6:50 am
by st.lisker
Hello. Is this some difference between 1 and 2 ? :
A a = (B)(I) b; //1
A a = (B) b: //2

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sat Jun 30, 2018 7:18 am
by admin
No, practically, there is no difference.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sat Jun 30, 2018 8:29 am
by st.lisker
Ok, thank you.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Mon Jul 09, 2018 8:21 am
by __JJ__
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.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sun Nov 25, 2018 6:26 am
by tosidis
Hello, I think that this a = (B)(A)b; would also compile and run , even if a is declared like this I a = new A();

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sun Nov 25, 2018 10:36 am
by admin
tosidis wrote:
Sun Nov 25, 2018 6:26 am
Hello, I think that this a = (B)(A)b; would also compile and run , even if a is declared like this I a = new A();
Correct.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sun Dec 23, 2018 6:41 pm
by jimmy21
Are there any differences betwen:

Code: Select all

 a = (B)(I)b; 
  a = (B)b;
  a = b;

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sun Dec 23, 2018 7:31 pm
by admin
(I) in (B)(I) is redundant so,
a = (B)(I)b; and a = (B)b; are same.

a = b; is obviously not the same because there is no cast. It is valid here only because type of b is-a A. It may not always work.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Mon Dec 24, 2018 3:32 am
by jimmy21
Thank you.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sun May 10, 2020 5:42 am
by noeloo
"A reference of type I can be cast to any class at compile time."

Are you sure about that?
I tried it and I get Inconvertible types compilation error.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Sun May 10, 2020 6:54 am
by admin
Please post the exact code that u tried. Also, please go through the above discussion.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Mon May 11, 2020 11:37 am
by noeloo

Code: Select all

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.

Re: About Question enthuware.ocajp.i.v7.2.1328 :

Posted: Mon May 11, 2020 12:15 pm
by admin
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.