• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting doubt

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.....
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic