0

I have a class BaseClass and a class Subclass which derives from Baseclass and overrides some methods of it.

public class BaseClass{}

public class SubClass extends BaseClass{

//override some methods
}

There is also another class SimpleBaseClass which derives from BaseClass and overrides some other methods of BaseClass, as it serves different functionality in comparison with SubClass

 public class SimpleBaseClass extends BaseClass{

    //override some other methods
    }

I want to confer in some cases the capabilities and features of the SubClass to the SimpleBaseClass. What is the best to do it? Is there a pattern to apply in that case? Simply though one can create a new SubSimpleBaseClass which derives from SubClass, copy the code of SimpleBaseClass and in this way has the functionality of both classes: SubClass and SimpleBaseClass.

 public class SubSimpleBaseClass extends SubClass{

        //do what SimpleBaseClass does
        }

But in this way code is repated in SubSimpleBaseClass. What is the best way to avoid it? Maybe Composition? Would the Decorator Pattern be an appropriate solution in this case?

6
  • Yes, Composition with dependency injection might help? Commented Jul 9, 2014 at 7:35
  • I think you need to reorganize your class hierarchy. If you need to make a not beautiful architectural/implementation decision upon implementation, then you need to reconsider your architecture. For instance make additional inheritance step and additional abstract classes which extend each other. Commented Jul 9, 2014 at 7:35
  • Without talking about patterns, if there is a commonality between SimpleBaseClass and SubClass, you can create a common ancestor for them which derives from BaseClass and let the others derive from that. Commented Jul 9, 2014 at 7:35
  • 1
    There are many ways to refactor code and restructure your modules. Design patterns help in finding what exactly to do. The composite pattern, the decorator pattern, the favor composition over inheritance idea, and so on, are very valuable tools for designing you software. But: What exactly to refactor is driven by the functionality you are trying to achieve. Or better: by the problem you are trying to solve. Technical issues should be never the motivation to refactor. Commented Jul 9, 2014 at 7:37
  • @Davio: The problem is that SimpleBaseClass and SubClass have no functionality in common implemented in them, they override different methods of parent. Moreover, the final purpose is to create a derived class which combines their functinalities (without repeating code). Commented Jul 9, 2014 at 9:14

1 Answer 1

5

Do not use a class hierarchy for the reuse of functionality. Rather use a delegation model where you refactor the functionality that is required for SubClass and your hypothetical SubSimpleBaseClass into another class. A simple example:

interface Handler {
  void foo();
}

public class BaseClass {

  private final Handler handler; // constructor omitted

  public void bar() {
    handler.foo();
  }
}

This allows you to implement a light form of "multiple inheritance" where you can still subclass BaseClass by multiple classes where you can still implement a common behavior for bar among multiple classes. This is a common recommendation, inheritance is often over-used.

If this is for some reason not possible (your example is not detailed enough to tell), rather refactor your code to use utility methods. Never copy&paste your code. This duplicates the maintenance costs.

Sign up to request clarification or add additional context in comments.

2 Comments

I like the delegation model, and i think the inheritance is too much used for factoring code.
I have finally it indeed so implemented, as recommended in your answer. Every functionality is moved to its class and all classes implement the same interface. The base class holds a reference of that interface and one can exchange the classes through a setter. In fact it ends to Strategy Pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.