Ramya Subraamanian wrote:F1, F2 and F3 throw classcastexception. F5 and F6 compile and run. F4, F7,F8 does not compile.
Ramya Subraamanian wrote:F1 - compiles because you can cast an interface to a class reference(only if the class it is not final). But throws a class cast Exception, because Breakable is not implemented, something like line1.
Ramya Subraamanian wrote:F2 and F3 - compiles but throws a classCast Exception because you are downcasting to the subclass SuperFancyCar. something like line2.
Ramya Subraamanian wrote:F4. ((FancyCar)car), though it is a valid cast. there is no turn() method in FancyCar, so does not compile.
F5. ((FancyCar)car), it is a valid cast. it has access to breakSmoothly() method of FancyCar, so compiles and runs.
F6. compiles and runs because Car class has Accelerate() method and calls it.
F7. doesnt compile because Car class does not have turn() method.
F8. doesnt compile because Car class does not have breakSmoothly() method.
Akshaya Thirusangu wrote:I understand F5 and F6 will compile and run.
F4, F7, and F8 will not compile.
and why does F2 and F3 throw ClassCastException in Runtime?
Akshaya Thirusangu wrote:I just am looking for a perfect explanation pertaining to inheritance IS-A test for this question.
Akshaya Thirusangu wrote:Roel, Your explanation have given me complete understanding on inheritance mechanism. Please keep doing this! Great Job!