Casting between the interface subclass and the superclass when the superclass does not implement the interface, but the subclass is

advertisements

In the following code

abstract class Vehicle { }
class Car extends Vehicle implements LandMover { }
interface LandMover { }
     Car porsche=new Car();
     LandMover lmv;
     lmv = porsche;
     Vehicle vec = (Vehicle)lmv;

Shouldnt there be a compiler error in 4th line as there is no relation between class vehicle and Interface LandMover?? and if there isnt what could be the reason.Thanks!!


The compiler only checks if there is a possible relationship, and there is one:

A LandMover could be a Car, which in turn IS-A Vehicle. Since you promise that this conversion is ok by using an explicit cast, the compiler is happy.