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.