Avoid no-op overrides

From CSSEMediaWiki
Revision as of 04:39, 7 October 2008 by Dominic Winkler (Talk | contribs)
Jump to: navigation, search

Don't do it.

"Don't do it" is what a no-op override is all about. What happens if you find that a class you are inheriting from provides a method with functionality that is inconvenient or unwanted? You might be tempted to remove that functionality by overriding the method with a blank method.

If you find yourself in this situation there are other things you should look at first. Inheritance is about Is-a relationships. This means that if your class Rabbit Is-a(n) Animal it can do all the things an animal can do. If you are inheriting functionality that is not wanted then you must ask are you inheriting from the right class.

No-op overrides break the Liskov substitution principle and Design by contract. A class should be able to use any subclass of a particular object without having to worry about whether that object will still act in an appropriate manner.

See also

Personal tools