aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting doubt" Watch "Casting doubt" New topic
Author

Casting doubt

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

Why is throwing an exception in line with //HERE comment?

>Exception in thread "main" java.lang.ClassCastException: Contact cannot be cast to Supplier
at Supplier.main(Supplier.java:14)

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Subclass reference variable cant refer super class object.Think about IS-A relationship here, Contact IS-A Supplier but Supplier is not Contact. You have to do instanceof test before down casting to enforce JVM to allow this,you cant directly downcast without it.


Tell the difficulties that i am difficult.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

You don't have to use instanceof. But it's a way of checking that the cast will work before you try it.

When you write Supplier s3 = (Supplier) c3, you're telling the compiler "trust me, I know that reference (c3) says it's a Contact object, but it's really a Supplier object, and I want you to treat it like that". So the compiler trusts you, because it might be (you'll get a compiler error if the cast can't possibly work). Supplier is a subclass of Contact, so it might be true.

But then you'll get a ClassCastException at runtime if the object isn't a Supplier object. This means you effectively lied to the compiler, but the runtime can actually check the type for itself.

And if we look at the rest of the code, we see c3 isn't a Supplier object after all. It's a plain Contact object (as set on line 8).

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 86

I don't understand what you mean with instanceof test.

If is a Supplier object, then IS-A Contact.
If is a Contact object then NOT IS-A Supplier so I have to do a Cast. Compilation succeeds but an exception is thrown at runtime.
Isn't that right?

What is what I have to test in that question?

That was a question of Sierra & Bates book and the answers were:

A. Compilation succeeds.
B. The code runs without exception.
C. If the line(s) of code that do NOT compile (if any) are removed, the code runs without exception.
D. If the line(s) of code that do NOT compile (if any) are removed, the code throws an exception at runtime. Correct
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Joey Sanchez wrote:I don't understand what you mean with instanceof test.

If is a Supplier object, then IS-A Contact.
If is a Contact object then NOT IS-A Supplier so I have to do a Cast. Compilation succeeds but an exception is thrown at runtime.
Isn't that right?

What is what I have to test in that question?

That was a question of Sierra & Bates book and the answers were:

A. Compilation succeeds.
B. The code runs without exception.
C. If the line(s) of code that do NOT compile (if any) are removed, the code runs without exception.
D. If the line(s) of code that do NOT compile (if any) are removed, the code throws an exception at runtime. Correct



yes you are right. what Matthew is saying, that in your case you can avoid that runtime ClassCastException by using instanceof test. instanceof is operator which checks for the IS-A relationship.
basically you could have done like this

if(c3 instanceof Supplier){ Supplier s3 = (Supplier) c3; }

here instanceof test will return false and you would have been saved from that illegal cast. what he meant by saying is that you don't need here , i think it would have changed the meaning of the question and also the answers.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:Subclass reference variable cant refer super class object.Think about IS-A relationship here, Contact IS-A Supplier but Supplier is not Contact. You have to do instanceof test before down casting to enforce JVM to allow this,you cant directly downcast without it.



hi saloni , but i think it should be other way around. Supplier IS-A Contact but not other way around.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:Subclass reference variable cant refer super class object.Think about IS-A relationship here, Contact IS-A Supplier but Supplier is not Contact. You have to do instanceof test before down casting to enforce JVM to allow this,you cant directly downcast without it.



hi saloni , but i think it should be other way around. Supplier IS-A Contact but not other way around.


What other way ? i am not getting .
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:
gurpeet singh wrote:
saloni jhanwar wrote:Subclass reference variable cant refer super class object.Think about IS-A relationship here, Contact IS-A Supplier but Supplier is not Contact. You have to do instanceof test before down casting to enforce JVM to allow this,you cant directly downcast without it.



hi saloni , but i think it should be other way around. Supplier IS-A Contact but not other way around.


What other way ? i am not getting .



i mean to say Supplier IS-A Contact but Contact IS-Not a Supplier.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

gurpeet singh wrote:
saloni jhanwar wrote:
gurpeet singh wrote:
saloni jhanwar wrote:Subclass reference variable cant refer super class object.Think about IS-A relationship here, Contact IS-A Supplier but Supplier is not Contact. You have to do instanceof test before down casting to enforce JVM to allow this,you cant directly downcast without it.



hi saloni , but i think it should be other way around. Supplier IS-A Contact but not other way around.


What other way ? i am not getting .



i mean to say Supplier IS-A Contact but Contact IS-Not a Supplier.


Oh i didn't consider.Mistyped badly.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

saloni jhanwar wrote:
gurpeet singh wrote:
saloni jhanwar wrote:
gurpeet singh wrote:
saloni jhanwar wrote:Subclass reference variable cant refer super class object.Think about IS-A relationship here, Contact IS-A Supplier but Supplier is not Contact. You have to do instanceof test before down casting to enforce JVM to allow this,you cant directly downcast without it.



hi saloni , but i think it should be other way around. Supplier IS-A Contact but not other way around.


What other way ? i am not getting .



i mean to say Supplier IS-A Contact but Contact IS-Not a Supplier.


Oh i didn't consider.Mistyped badly.


happens.....
sandeep ss
Greenhorn

Joined: May 16, 2012
Posts: 1
Joey Sanchez wrote:Why is throwing an exception in line with //HERE comment?

>Exception in thread "main" java.lang.ClassCastException: Contact cannot be cast to Supplier
at Supplier.main(Supplier.java:14)



Supplier s2 = (Supplier) c1; ---- is internally ----Supplier s2 =s1
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting doubt