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?
beautifularchitectural/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.SimpleBaseClassandSubClass, you can create a common ancestor for them which derives fromBaseClassand let the others derive from that.