Tell the difficulties that i am difficult.
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
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.
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.
Tell the difficulties that i am difficult.
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 .
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.
Tell the difficulties that i am difficult.
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.
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)