Empty method over-rides

From CSSEMediaWiki
Revision as of 06:39, 1 August 2009 by Aidan (Talk | contribs)
Jump to: navigation, search

Empty method overrides occur when some part of an interface is not relevant to some specific implementation. This can occur when implementing interfaces or inheriting from some class. In this situation it is tempting to override the method with an empty implementation which does nothing. Often the goal is to maintain polymorphism despite the fact that it would seem the interface doesn't fit well.

Example

Empty-method-override.png

  • The Dog class has a basic bark() method.
  • This has been overridden to give slightly different behavior in the LoudDog and QuietDog classes.
  • QuietDogs don't bark at all, so the QuietDog.bark() method is empty.

Possible Solutions

  • Allow empty method overrides
  • Allow empty method overrides, but the empty method should throw an exception. In many cases this would require the use of the instanceof operator. This destroys polymorphism.
  • Extend the hierarchy. Break the interface up so that empty method overrides are not necessary. This will add extra interfaces to the mix and will destroy polymorphism in some cases.
Personal tools