can base class contain protected methods that are not to be used by all descendant classes?
I have this situation:
- EntityA: has some methods that handle activities
- EntityB: has some methods that handle products
- EntityC: has methods that handle both activities and products
I use C#, so multiple inheritance is not possible.
Is it OK to have a base class with protected methods that deal with both activities and products, and have all 3 classes inherit from this base class?
Edit: I never understood what do we gain by using composition instead, how do you implement such approach in a more complex scenario. Let me give you a bit more expanded situation:
- EntityA: needs to use internally FilterActivitiesByType() method, and has public methodsA1(), A2(), A3()
- EntityB: needs to use internally FilterProductsByType() method, and has public methodsB1(), B2(), B3()
- EntityC: needs to use internally both FilterActivitiesByType() and FilterProductsByType(), and has public methodsC1(), C2(), C3()
So, both FilterActivitiesByType and FilterProductsByType are not to be exposed through interface. These methods can in many cases be even static. I can now put those two methods in IFilterActivityHelper and IFilterProductHelper classes, respectively, and use DI in constructor to make it available to EntityA, EntityB and EntityC classes. This is, I believe what you all advise by composition.
Now, Imagine that I have several activity helper methods, and several product helper methods. I could put them in each own specific helper class (1st), or I could put all activity helper methods in a single class ActivityHelper (2nd). (1st) Means that I will have lots of instances to inject in constructor, for each separate helper method. (2nd) Gives us the the same situtation as in my original question with inheritance, since I now have IActivityHelper inserted in ActivityA, and ActivityA needs to use only 2 methods out of 5 that it exposes.
So, can someone elaborate how to do the composition properly in such cases?