I've an interface (IService), an abstract base class (BaseService), an implementation class (ServiceImpl) and a test class (TestService). Here is what I am trying to do,
1) BaseService implements IService
2) ServiceImpl extends BaseService implements IService
3) TestService constructor takes IService as argument
4) In Spring application context load TestService injecting ServiceImpl in its constructor
5) In TestService constructor I cast the injected impl class to the BaseService class.
Now, at step 5 I get "ClassCastException". I know there are some workarounds, but I am just trying to understand why wouldn't Spring allow this ?
AbstractBase implements Iface
ServiceImpl extends AbstractBase implements IFace
TestService(IFace iService)
{
AbstractBase base = (AbstractBase)iService; //ClassCastException ??
}


Reply With Quote