Page 1 of 1

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Fri Sep 27, 2013 7:10 am
by Wisevolk
Hi,
I'm a little bit angry with multi casting !!
So, if I understand the order is important (B)(I)b is not the same as (I)(B)b ?
Thnks

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Fri Sep 27, 2013 7:54 am
by admin
To understand multiple casts, just break them up, and check the validity of each individual cast. For example, (B)(I)b can be broken up like this:

I tempI = (I)b;
B tempB = (B) tempI;

and (I)(B)b can be broken up as:

B tempB = (B)b;
I tempI = (I)b;

Now, you can easily figure out which casts are valid and which are not and whether order is important or not.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Sun Sep 29, 2013 7:34 am
by Wisevolk
Ok I'll try to keep this method in my brain !!
Thnks

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Mon Mar 23, 2015 12:59 am
by pfilaretov
The explanation says: "A reference of type I can be cast to any class at compile time."
Why is it so? Why compiler can't figure out which class not is-a I?

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Mon Mar 23, 2015 4:42 am
by admin
Because an interface can be implemented by any class. So at run time the reference may point to an instance of a sub-class that extends a class and also implements the interface. That is why even though the compiler knows that a particular class does not implement I, it cannot reject the cast. A subclass of that class may implement the interface.

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Tue Mar 24, 2015 12:40 am
by pfilaretov
admin wrote:A subclass of that class may implement the interface.
That's the point I forgot, thank you!

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Sat Jan 21, 2017 12:15 pm
by swathi12
Hi,

When i executed the below code, i get "class cast exception that B cannot be cast to I". b can be casted to I and I can be casted to B and since B is a A, it should run without exception. So why am I getting this error.

Can you please help me understand, why is the compiler throwing class cast exception.

interface I {}
public class A implements I1
{
public static void main(String[] args)
{
A a=new A();
B b=new B();
a=(B)(I)b;

}
}

class B extends A { }

class C extends B { }

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Sat Jan 21, 2017 12:28 pm
by swathi12
Sorry, got it. no problem.

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Wed Oct 04, 2017 4:41 am
by horst1a
How do i check the validity of each cast ? What criteria or questions do i have to ask in order to decide if a cast is right or not ? Is it always the Is-A relationship ?
i.e:
a = (B)(I)b;

Where will i start and how will i proceed ?

Re: About Question enthuware.ocpjp.v7.2.1578 :

Posted: Wed Oct 04, 2017 4:55 am
by admin
horst1a wrote:How do i check the validity of each cast ? What criteria or questions do i have to ask in order to decide if a cast is right or not ? Is it always the Is-A relationship ?
i.e:
a = (B)(I)b;

Where will i start and how will i proceed ?
Which book are you following?
If you are not following any book, try this article: https://howtoprogramwithjava.com/java-cast/