Avoid no-op overrides

From CSSEMediaWiki
Jump to: navigation, search
It should be illegal for a derived class to override a base class method with a NOP method, i.e. a method which does nothing. --Riel's Heuristic 5.17, Arthur Riel 1996

A no-op override is a method defined in a subclass with the express purpose of removing behaviour inherited from a superclass.

"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