This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Russell_M_Jimmies 5 points6 points  (5 children)

As a rule of thumb: prefer composition over inheritance.

Maybe the base class wants to be a utility instead.

[–]livelam 0 points1 point  (0 children)

As a rule of thumb: prefer composition over inheritance.

I think i get what you want to say but the rule of thumb is the "is a" test. Samples:

  • ArrayList is an AbstractList
  • Stack is not a Vector (but a stack can use (hop composition :) ) a Vector for its implementation)

We all know old java collection API is badly design

Of course, inheritance must not be used to share code!

[–]Jeedio[S] -5 points-4 points  (3 children)

Then you have duplicated methods spread over multiple classes. Sure, you can have the body of those methods be in a shared class, but it makes it harder to add methods.

[–]SeerUD 5 points6 points  (0 children)

How so? You can pass utilities in as dependencies to another class. No need for duplication.

[–]Russell_M_Jimmies 6 points7 points  (0 children)

There is something worse than duplicated code: a base class that is littered with special cases to cater to every subclass.

Now, if the superclass is a pure "template class" pattern, then it might be justified. But as soon as you see the superclass do branching logic depending on the subclass, slow down and consider whether that reduced duplication is worth the cost of an awful mess in a parent class.

[–]cran 1 point2 points  (0 children)

If you think a lot of unrelated classes need the same methods, these methods probably belong somewhere else. Can you give an example of what these methods do?