Announcement Announcement Module
Collapse
No announcement yet.
Classcastexception using inheritance Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Classcastexception using inheritance

    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 ??
    }

  • #2
    1. If BaseService is Abstract, it's a "best practice" to begin its name with "Abstract" (e.g. AbstractService).
    2. If ServiceImpl extends BaseService, which already implements IService, there is no need for ServiceImpl to also implement IService.
    3. Have you thought about using JUnit?
    5. Why? Why not TestService( BaseService service )?

    Comment


    • #3
      Is Spring proxying the injected IFace? If so that would explain things.

      Can you find the classname of the injected IFace please.

      Comment


      • #4
        No I am not proxying the IFace. How would I use proxying in this case ? How would it help ? Any example ?

        In this example (class names are just made up), I am injecting ServiceImpl for the IFace in the TestService class.

        Comment


        • #5
          Based on what you have stated you are injecting something that implements IFace, but it doesn't implement AbstractBase. This can happen if you proxy something (to add transactional behavirour for example). Spring will create an object that "looks like" IFace but is actually a proxy which wraps your implementation of IFace, so when you call IFace.doSomething you are actually calling that on the spring proxy which (for example) starts a transaction, delegates the call to your implementationIFace.doSomething and then when that returns then (for example) closes the transaction.

          Why are you casting to the implementation class anyway?

          Comment


          • #6
            Here it is again,

            1) AbstractBase implements Iface
            2) ServiceImpl extends AbstractBase implements IFace

            TestService(IFace iService)
            {
            AbstractBase base = (AbstractBase)iService; //ClassCastException ??
            }

            Using Sping I am just injecting IFact to the TestService. I haven't defined any proxy or anything else. I am not doing any transaction.

            I need to cast the impl to the abstract class, because the abstract class provides methods which are not available in the IFace interface. But, when I do this casting I am getting the ClassCastException.

            Now if I don't use Spring it works ok, it's only when inject the impl using Spring that I get the ClassCastException.

            Any suggestions?

            Comment


            • #7
              Maybe output iService.getClass() and see what the actual class is. That might help to track down the problem.

              Regards,
              Andreas

              Comment

              Working...
              X