when hierarchy smells?

Overridden method without a call to parent is always a lame-hierarchy warning to me. It is so simple: The proper hierarchy needs ‘is-a’ relations. Is-a means I can substitute all base classes with their sub-types (yes, that’s the Liskov’s Substitution Principle)… Now, is it really an is-a if subtype doesn’t call super? Not at all – it is rather clear message that subclass want to do something very different than the base class…

Inheritance is a tricky business, it’s wise to remember Design By Contract Principle:

A sub-type can only have weaker pre-conditions and stronger post-conditions than its base class.

On the other hand, if overridden method is truly LSPy without calling super it likely means code duplication smell.

Missing super.myMethod() in myMethod() is a potential smell. It’s easy to spot in java code when methods are annotated with @Override.

(Update 04-10-2007)

I’ve just had a debate with my friend but my stance is still the same. No super() usually means one of those:

  • LSP violation
  • duplication

Comments are closed.

%d bloggers like this: