wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ExamLab Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ExamLab Question" Watch "ExamLab Question" New topic
Author

ExamLab Question

Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208



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 ???


OCJP-90%,OCPWCD-95%
Piyush Joshi
Ranch Hand

Joined: Jun 10, 2011
Posts: 207

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.


Piyush
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

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 ??
shuba gopal
Ranch Hand

Joined: May 12, 2011
Posts: 76
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

Joined: Jun 10, 2011
Posts: 207

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

Joined: Jun 07, 2011
Posts: 208

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

Joined: Jun 07, 2011
Posts: 208



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

Joined: Jun 10, 2011
Posts: 207

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.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

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'


OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Sagar Shroff
Ranch Hand

Joined: Jun 07, 2011
Posts: 208

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

Joined: Jun 10, 2011
Posts: 207

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

Joined: Jun 07, 2011
Posts: 208

Thanks Piyush !!!
 
 
subject: ExamLab Question
 
Similar Threads
Assignment problem
Assignment funda not clear
Assigning references problem
Reference variable casting w.r.t interfaces
examlab - 1 [reference variable casting]