About Question enthuware.ocpjp.v7.2.1578 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

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

Post by Wisevolk »

Ok I'll try to keep this method in my brain !!
Thnks

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

pfilaretov
Posts: 35
Joined: Mon Jul 28, 2014 2:05 am
Contact:

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

Post by pfilaretov »

admin wrote:A subclass of that class may implement the interface.
That's the point I forgot, thank you!

swathi12
Posts: 2
Joined: Sun Jan 08, 2017 1:16 pm
Contact:

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

Post 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 { }

swathi12
Posts: 2
Joined: Sun Jan 08, 2017 1:16 pm
Contact:

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

Post by swathi12 »

Sorry, got it. no problem.

horst1a
Posts: 37
Joined: Mon Jun 12, 2017 2:16 am
Contact:

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

Post 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 ?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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/

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests