• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

ExamLab Question

 
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am not able to understand how Line 17 works.There is no relationship between the two.Then why is it compiling.I think i am having some wrong concepts here please can someone solve it ???
 
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look closely, you'll find that from line 14 onwards you are casting only null.
null can be cast to anything but you can assign the casted reference to only a reference falling in same hierarchy. So everything is legal from line 14 onwards as we are in a single hierarchy.
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My Understanding of casting says that you can cast i.e upcast or downcast if there is some relationship between the two.I mean Parent child relationship.Now in this problem we are assigning "m" to "a",there is no relationship between the two.Then also it is compiling.I know i am having wrong concepts here that's why this is compiling so please HELP ??
 
Ranch Hand
Posts: 87
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During compilation: Min implements B. So m can be upcast to A for the compilation to work do you agree? Instead of null if you had Min m = new B(), compilation would succeed for A a2 = (A) m. But there will be ClassCastException during runtime.

During runtime: Since m is null ClassCastException is not thrown as m is not found to be pointing at any inappropriate object.

 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Shroff wrote:My Understanding of casting says that you can cast i.e upcast or downcast if there is some relationship between the two.I mean Parent child relationship.Now in this problem we are assigning "m" to "a",there is no relationship between the two.Then also it is compiling.I know i am having wrong concepts here that's why this is compiling so please HELP ??


lets explore what can be done in casting:
we start with some modifications in your example:


I think it solves your confusion. Does'nt it?
If you want to learn more about type conversions read Conversions and Promotions
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shuba gopal wrote:During compilation: Min implements B. So m can be upcast to A for the compilation to work do you agree? Instead of null if you had Min m = new B(), compilation would succeed for A a2 = (A) m. But there will be ClassCastException during runtime.

During runtime: Since m is null ClassCastException is not thrown as m is not found to be pointing at any inappropriate object.




Thanks shuba i got it !!
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So based on this i have analyzed 1 thing is that casting is based on the object because m is referring to b object and there is a relationship between class B and A...
Am i right ??.....Correct me if i am wrong.....
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sagar Shroff wrote:

So based on this i have analyzed 1 thing is that casting is based on the object because m is referring to b object and there is a relationship between class B and A...
Am i right ??.....Correct me if i am wrong.....


There is no need for a relationship between class B and A, because Min is an Interface.
If you remove B extends A from your code then also this cast will work, try to do it. (it will compile successfully).
Just imagin for a moment that B and A are unrelated, and change your code to this:

now A and B are unrelated, now also you can write:

and compiler will not complain. Just try to write the above code and see for yourself.

Why? because during runtime m could be reassigned to a subclass of A which implements Min.
Therefore at runtime there is a possibility of this cast to succeed.
So compiler intelligently defers type checking to runtime, and you do not get any compile error.

But if Min was a class

and you try to do the same:

now compilation will fail. because compiler knows that for this cast to work m should be reassigned to a subclass of A which also extends Min.
but compiler also knows that this is not going to happen, because any class can extend only one class.

So there is difference how compiler handles interface casting. Do you now get my point?
I suggest you do the code changes and try to experiment.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Piyush... Your analysis is perfect and the Java Language Specification explains it like that. Here is a caption: "The cast is always legal at compile time (because even if S does not implement T, a subclass of S might)."

HtH

Ikpefua

P.S If you click on the link, you need to scroll down the page to a sub-title called; 'CASTING CONVERSION'
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Piyush you banged it !!!...i have understood what you were trying to tell !!.....But just 1 thing you said at runtime m could be reassigned to a subclass of A which can implement Min.....now all assignments are made at compile time......so how can you assign sometime at runtime can you give me 1 example Please...???.....
 
Piyush Joshi
Ranch Hand
Posts: 207
jQuery Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At compile time we can only be sure of the type of a variable, but the actual object referenced by that variable is only decided at runtime.

For example:

at compile time it is certain that type of variable m is Min and type of variable a2 is A. but the type of object referenced by variable m depends on runtime.

So for casting conversions, compile time rules only check on the basis of the type of variables,
if compilation succeeds then in some cases a run time check is required to be performed on the actual objects referenced by the variables.

So when you are checking that a cast will compile or not: use the types of variables not the type of objects referred by them.
 
Sagar Shroff
Ranch Hand
Posts: 211
Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Piyush !!!
 
My pie came with a little toothpic holding up this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic