Ankur kothari wrote:
can anyone explain why i get an error at line 2 and not at 1?
without the cast both give incompatible types...but with cast i get an error at 2...(convertible type) but not at 1
You get an compile time error at line 2 and not at line 1.This is right.
At line 1 the interface object it gets executed successfully because Car is an
instance of Move Interface which returns true and hence the handle of the interface
can point to Car.
rohan yadav wrote:I didnt get it how car is an instance of move??since car not implementing move??
rohan yadav wrote:Is it ok to cast a car object to Drive refrence variable??
rohan yadav wrote:
Now i understand we can assign any class object to any interface refrence variable through casting.
But we cant do it for classes(in this case vehicle and car).
Am i right???
Ankur kothari wrote:hey actually the compiler just assumes that the casting will work but you are bound to get a runtime exception if at runtime the casting is not proper...
why we cant cast car into vehicle class ?
Why we can cast class car into Move ?
Ankur kothari wrote:hey punit that was beautiful...believe me....but why cant the compiler do checking if there is a heirarchy relationship between move and car subclasses?
i think i missed it?
Ankur kothari wrote:you mean to say that Move m =(Move)Car is allowed because it might happen that car variable is itself referring to a subclass that implements move?
rohan yadav wrote:Thanks Punit. Great Explanation man. I must say!!!
Where did You find these kind of stuff?? I mean what is the reference through which you study all this??
What material i have to study so that i can master java like urself??
Currently i am preparing for SCJP exam
rohan yadav wrote:Exactly Ankit i totally agree with. They are genius. I want my programming to become like them
Slime does not pay. Always keep your tiny ad dry.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton