0

Is inheritance chaining something that should be avoided?

I'm currently in the process of refactoring a java project and I have come across a number of classes (8) which all of them have a shared functionality. So, using the Extract Super Class refactoring, I created a super class with that functionality and passed it to all the sub-classes. Now, 2 of the classes have also a second shared functionality. Should I create another super class which inherits from the first super class and, in turn, is extended by the sub-classes or simply extend multiple classes on the 2 sub-classes?

Thoughts

Even though both options would work, I feel that going for multiple extensions is preferable since it describes and clarifies the responsibilities of the class. Also, we would avoid making continuous super() calls in order to reach the top super class.
On the other hand, maybe having an inheritance chain kinda tidies up the place.

Edit: I understand I stated my problem completely wrong. The 8 classes that are mentioned above are made to abide by the command pattern and as such each one has a different purpose and functionality. I have extracted a super class to avoid code duplication inside their constructors. Two of those classes have also have a little bit more duplicate code (not duplicate functionality, in the sense that they do the same thing). Unfortunately, due to the nature of the project, the fact that I did not write the original code and the tight time schedule do not allow me to completely reconstruct the whole project. It's, let's say, beyond repair for now. But what I can do is patchwork to try to remove duplicate code and tidy the place up a bit.

Invalid_Path
  • 321
  • 3
  • 8
  • It's hard to give advice without a concrete example. It's very likely that composition would be a better pattern than either of the two options you've given. Think really hard about whether you're dealing with "has a" rather than "is a" relationships. If you can give a concrete example, someone will be able to give a clear answer about which pattern (composition vs chaining vs multiple extensions) makes the most sense for your case. – Dawood ibn Kareem Dec 18 '19 at 20:39
  • Edited my question in order to possibly offer some clarification. – Invalid_Path Dec 18 '19 at 20:59
  • It sounds like there is no "is a" relationship here, and you should probably try composition. But again, without seeing your code, it's hard to answer definitively. – Dawood ibn Kareem Dec 18 '19 at 21:02

2 Answers2

1

Super classes should be used when they model a is-a relationship. If the 8 classes are the same entity, a common super class can be used to avoid duplication. Class hierarchies can hurt maintainability quickly and should be as simple as possible. Avoiding duplicate code is not the main concern when introducing them.

As @davidza5 already pointed out, the principle of composition over inheritance applies here. Common functionality should preferably be extracted into classes that model a has-a relationship, which decouples functionality and makes it easier to change later on.

Another aspect: if a refactoring does not work in all cases this does not mean it is the wrong idea. If it leads to good results for the majority, working around the quirks of the exceptions is a good trade off to make.

ldz
  • 2,217
  • 16
  • 21
  • I edited the post accordingly to avoid any further confusion. I feel your final paragraph captures my situation right now. Also learned to always prefer composition over inheritance thanks to you and @davidza5. – Invalid_Path Dec 18 '19 at 20:58
0

Both methods seems to be a bad practice in most cases (favorize composition over inheritance). If you can avoid it by using design patterns (facade, composite, adapter,decorator...). If it can't be done in your case the principle of encapsulation and inheritance is what I would apply. So, when necessary chain inheritance.

davidza5
  • 86
  • 1
  • 10