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.
Sage of The Monstrous Toad of Mount Myoboku
rohan yadav wrote:I didnt get it how car is an instance of move??since car not implementing move??
Sage of The Monstrous Toad of Mount Myoboku
rohan yadav wrote:Is it ok to cast a car object to Drive refrence variable??
Sage of The Monstrous Toad of Mount Myoboku
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???
Sage of The Monstrous Toad of Mount Myoboku
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...
Sage of The Monstrous Toad of Mount Myoboku
Sage of The Monstrous Toad of Mount Myoboku
why we cant cast car into vehicle class ?
Why we can cast class car into Move ?
Move m1=(Move)car;//1
SCJP 6
SCJP 6
Sage of The Monstrous Toad of Mount Myoboku
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?
SCJP 6
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?
SCJP 6
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
SCJP 6
Sage of The Monstrous Toad of Mount Myoboku
rohan yadav wrote:Exactly Ankit i totally agree with. They are genius. I want my programming to become like them
SCJP 6
Sage of The Monstrous Toad of Mount Myoboku
Yeast devil! Back to the oven that baked you! And take this tiny ad too:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|