3 Replies Latest reply on Mar 4, 2013 6:49 PM by marlin

    Help with inheritance design issue

      Hi everyone, this is the first time I post in this or any forum, so I hope I am doing it the correct way.

      I have a problem at work with a bug where the application is throwing a ClassCastException, this is the scenario: The application authorizes transactions like sales, refunds, rollbacks. There is a base core module, which we are not allowed to modify, just use the classes in it and derive from them, so we have some POJOs that are used to store the transaction information, the basic structure is like this:
      public class BaseTransaction{
      public class ModuleTransaction extends BaseTransaction{
      public class BaseRefundTransaction extends BaseTransaction{
      public class ModuleRefundTransaction extends ModuleTransaction{
      In the code, ModuleRefundTransaction uses a lot of things that come from ModuleTransaction, that´s why it is extending it and not BaseRefundTransaction .

      The problem is that the base module does a lot of processing for refunds so, it is doing stuff like:
      BaseRefundTransaction  refundTxn = (BaseRefundTransaction )helper.getRefundTransaction();
      and there we get the ClassCastException, because our helper is returning a ModuleRefundTransaction not a BaseRefundTransaction.

      I have to fix this bug, but I don´t see any elegant solution, I think that the whole thing has a bad design, but maybe I am not experienced enough to see a good solution. The main problem is that we can´t change anything in the base module.
      My first lazy thought was to start copying all the code in the base module into our module and use our class, but that's just not good.

      Any suggestions? Thank you very much.
        • 1. Re: Help with inheritance design issue
          Not sure what code you can change and what not. Nevertheless, I will try. Why not use delegation, i.e. make ModuleRefoundTransaction have a BaseRefundTransaction field. Inheritance is not the only way to achieve code reuse.
          Your line becames:
          BaseRefundTransaction  refundTxn = helper.getRefundTransaction().getBaseRefundTransaction();
          • 2. Re: Help with inheritance design issue
            The classes I can´t change are the ones with base on the name, BaseTransaction and BaseRefundTransaction, and also the classes that are using the helper to get a BaseRefundTransaction. I can´t either change the helper, that's part of the framework which handles all the instantiation of classes around the aplication
            • 3. Re: Help with inheritance design issue
              The error message is correct. You can't do that cast. The ModuleRefundTransaction returned by your helper does not descend from a BaseRefundTransaction so a MRT is NOT a BRT.

              The problem is that in the process of extending BT to BRT you could have added some fields none of which were added to BT when you moved to MT and then to MRT. Thus if you have an MRT you can not call it a BRT because those fields would be absent.

              Difficult to say what you could do about it without knowing what you are trying to preserve. You got a MRT back from your helper, you seem to want a BRT for some purpose. Since you can't cast, perhaps you can instead construct a new BRT and move the appropriate fields from you MRT to your BRT.