Question

Let's say that I have an Interface C and a class A which implements it.

Now assume that that I want to change the implementation of A to all functions in C, except one, with the class B. Should I extends A and override C or should I implement C as a new independent class?

Lets say that some phones company wants to to implement their basic interface Phone and they already have some old implementation of OldPhone, which they want to keep all of its functionalities: Make a call, hung up, send SMS- and they don't want to change all of it but they do want other new functionalities to make their new Smart Phone- should they extend OldPhone or make a new Class?

I would like to know when should I extend classes and when should I implement interfaces from the beginning- on the one hand I don't want to copy code but on the other one sometimes the types ,even though they are both an A, are conceptually different.

Was it helpful?

Solution

If you have code which you want to reuse, then extending is the better choice.

But ask yourself: is the new phone a better version of the old one or a completely new one just with similiar functions?

  • In the first case: extending.
  • In the second abstraction is what you're searching for: extract the functions the have in common and put them in an abstract class, which both classes will extend.

OTHER TIPS

If there is shared functionality in OldPhone and SmartPhone you could extract that into a (abstract)class and let both extend that and let them have the other as diffrences.

Otherwise go for interface

It rather depends on how much changes from C (old phone) to B (new phone). Using phones as an example is quite good because an old phone conceptually does the same as a new phone - it makes phone calls.

The difficultly comes when the implementation of C is tied very tightly to how the phone works. Lets say for example that the old phone insists on using a circular ring that you turn to select the number to dial rather than a key pad. In that situation, if you have complete control of the code, then you might want to try and put in a layer of abstraction between the interface and the old phone. The abstraction layer would capture just what it means to be a phone and nothing more.

Once you have the abstraction layer you can then choose whether you extend the old phone to create your new phone or create a new implementation. Either way it should be easier than you have now as the old phone will contain far less code and only those bits that are specific to it.

Now See your Requirements says B should behave exactly like A for most of the functionality. Now suppose you choose to implement i.e B implments C. Then if in future Class A logic Changes then you also have to change class B. So its not good for your requirement.

Now Suppose you extends A. But then you are tighly coupled with A. as if A changes your Big will aslo behave the same way.

So try to get the best of two words use composition. B implements C and use A as an delegate to implement those methods. My answer is for your case it not necessarily be the best choice in every case.

Rule of thumb, you should extend a class only if there is a parent-child relationship. Otherwise go for interface implementation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top