Call super

Call super

Call super is a code smell or anti-pattern of object-oriented programming, i.e. a bad solution to a common design problem. Call super requires users of a particular interface to override the method of a superclass and call the overridden from the overriding method.

Description

In object-oriented programming, users can inherit the properties and behaviour of a superclass in subclasses. A subclass can override methods of its superclass, substituting its own implementation of the method for the superclass's implementation. Sometimes the overriding method will completely replace the corresponding functionality in the superclass, while in other cases the superclass's method must still be called from the overriding method. Therefore most programming languages require that an overriding method must explicitly call the overridden method on the superclass for it to be executed.

The call super anti-pattern relies on the users of an interface or framework to derive a subclass from a particular class, override a certain method and require the overridden method to call the original method from the overriding methodcite web
url=http://www.martinfowler.com/bliki/CallSuper.html
title=MF Bliki: CallSuper
author=Fowler, Martin
] :

This is often required, since the superclass must perform some setup tasks for the class or framework to work correctly, or since the superclass's main task (which is performed by this method) is only augmented by the subclass.

A better approach to solve these issues is instead to use the template method pattern, where the superclass includes a purely abstract method that must be implemented by the subclasses and have the original method call that method:

Language variation

The appearance of this anti-pattern in programs is usually because few programming languages provide a feature to contractually ensure that a super method is called from a derived class. One language that does have this feature, in a quite radical fashion, is BETA. The feature is found in a limited way in for instance Java and C++, where a child class constructor always calls the parent class constructor.

Languages that support "before" and "after" methods, such as Common Lisp, provide a different way to avoid this anti-pattern. The programmer can, instead of overriding the superclass's method, supply an additional method which will be executed before or after the superclass's method.

Example

Suppose there is a class for generating a report about the inventory of a video rental store. Each particular store has a different way of tabulating the videos currently available, but the algorithm for generating the final report is the same for all stores. A framework that uses the call super anti-pattern may provide the following abstract class (in C#):

abstract class ReportGenerator { public virtual Report CreateReport() { // do some calculations // ... return new Report(...); } }

A user of the class is expected to implement a subclass like this:

class ConcreteReportGenerator : ReportGenerator { public override Report CreateReport() { // tabulate data // ... return base.CreateReport(); } }

A preferable interface looks like this:

abstract class ReportGenerator { public Report CreateReport() { Tabulate(); // do some calculations // ... return new Report(...); } protected abstract void Tabulate(); }

An implementation would override this class like this:

class ConcreteReportGenerator : ReportGenerator { protected override void Tabulate() { // tabulate data // ... } }

References

External links

* [http://beust.com/weblog/archives/000252.html Otaku, Cedric's weblog: The "call super" anti-pattern]


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Super League play-offs — Super League logo Since 1998 a play off system has been used to determine the Super League champions. The format has changed over the years, starting with a play off involving first five, then six teams and currently eight. The play offs… …   Wikipedia

  • Super Bowl XXII — Infobox SuperBowl sb name = XXII visitor = Washington Redskins home = Denver Broncos visitor abbr = WAS home abbr = DEN visitor conf = NFC home conf = AFC visitor total = 42 home total = 10 visitor qtr1 = 0 visitor qtr2 = 35 visitor qtr3 = 0… …   Wikipedia

  • Super Bowl XL — Seattle Seahawks Pittsburgh Steelers (NFC) (AFC) …   Wikipedia

  • Super Trouper (album) — Super Trouper Studio album by ABBA Released November 3, 1980 …   Wikipedia

  • Super Robot Taisen OG Saga: Endless Frontier — Developer(s) Banpresto, Monolith Soft …   Wikipedia

  • Super Adobe — is a form of Earthbag Construction that was developed by Iranian architect Nader Khalili. The technique uses long snake like sand bags to form a beehive shaped compressive structure that employs arches, domes, and vaults to create single and… …   Wikipedia

  • Super RTL — Senderlogo Allgemeine Informationen Empfang …   Deutsch Wikipedia

  • Super Black Market Clash — Album par The Clash Sortie 26 octobre 1994 Enregistrement Février 1977 mai 1982 Durée 77 min 13s Genre Punk rock …   Wikipédia en Français

  • Call of Duty: Modern Warfare 2 — Разработчик Infinity Ward …   Википедия

  • Call of Duty: Modern Warfare 3 — Call of Duty Modern Warfare 3 Обложка игры Разработчик Infini …   Википедия

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”