Akash Narayan
Akash Narayan

Reputation: 310

Inheriting an implementation class for a method reusability

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-

Upvotes: 0

Views: 34

Answers (1)

Steve
Steve

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

Related Questions