Reputation: 310
There is an implementation class - Class P, which has around 10 concrete methods in it. My interest lies with one of the methods of this class.
Problem is my class is not encouraged to directly call Class P method. Also for my class to access Class P method i will need to add a condition in the desired Class P method.
My question is which is more subtle-
inherit Class P into a new impl class and use this (given the no of methods in this class P i am not sure if this is right, as i will have overhead of unwanted extra methods).
or adjust my operation in the Class P method by adding few lines in Class P method.
Upvotes: 0
Views: 34
Reputation: 989
If you need the method to be modified then it's not strictly reusable as is. Inheriting a class would provide the method as is, then you'd need to override it with a new version anyway. If you need to add code to execute before or after you could call the super but you could do the same thing by instantiating class P and calling it's method.
In addition inheriting for this purpose may make your intentions unclear as the next time someone or you looks at the code they'll ask, "why is class b a class p?".
Instead of achieving reusability through inheritance in this case, try componetizing the parts of the original method that wouldn't change and use those components in both classes.
Upvotes: 1