1

I've encountered code where a base class implements an interface, and then all the subclasses also declare they implement the interface like so

class BaseClass implements Countable { public function count() {} }
class SubClass extends BaseClass implements Countable {}

Assuming the absence of a namespace and use statements, to me, the latter implements Countable is egregious. By virtue of extension it already does.

One might argue this makes it clear the subclass implements Countable without having to look at the base class, but to me it's duplicate code that could lead to longer refactoring times (albeit nominal).

Is this practice considered good form for any reason, or is my hunch correct?

quickshiftin
  • 66,362
  • 10
  • 68
  • 89
  • 4
    As you say, there's no technical reason to do so whatsoever. The rest is rather opinion-based. Personally I agree that it's madness. – deceze Nov 04 '16 at 15:26
  • I wouldn't do it, either. I don't think the safety is worth the redundancy. There was a good discussion of the pros and cons here: http://stackoverflow.com/questions/5668429/why-would-both-a-parent-and-child-class-implement-the-same-interface –  Nov 04 '16 at 15:27
  • Personally, I see no reason why it would be good practice, it's completely redundant and by definition - useless. It just adds to complexity for no gain whatsoever. I'm in the same boat as @deceze on this one, I can't think of a single scenario where this approach would prove useful. – Mjh Nov 04 '16 at 15:30
  • Hopefully there are no `namespace`s and `use`s anywhere in the encountered code. Because in different namespaces you may implement different Countable interfaces on diff includes via `use` – JustOnUnderMillions Nov 04 '16 at 15:35
  • @JustOnUnderMillions Good point, I'll revise the question – quickshiftin Nov 04 '16 at 18:57

0 Answers0