About Question enthuware.ocpjp.v7.2.1578 :
Moderator: admin
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
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
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
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
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.
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.
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
Ok I'll try to keep this method in my brain !!
Thnks
Thnks
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
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?
Why is it so? Why compiler can't figure out which class not is-a I?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
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.
-
- Posts: 35
- Joined: Mon Jul 28, 2014 2:05 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
That's the point I forgot, thank you!admin wrote:A subclass of that class may implement the interface.
-
- Posts: 2
- Joined: Sun Jan 08, 2017 1:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
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 { }
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 { }
-
- Posts: 2
- Joined: Sun Jan 08, 2017 1:16 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
Sorry, got it. no problem.
-
- Posts: 37
- Joined: Mon Jun 12, 2017 2:16 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
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 ?
i.e:
a = (B)(I)b;
Where will i start and how will i proceed ?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1578 :
Which book are you following?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 ?
If you are not following any book, try this article: https://howtoprogramwithjava.com/java-cast/
Who is online
Users browsing this forum: No registered users and 29 guests