Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting doubt

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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)

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.....
 
sandeep ss
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic