call super
≈ 2 min read
Names in other languages
- Magyarfelfelé hívás
- 日本語スーパークラスの呼び出し
- Englishcall super
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
Call super is a code smell or anti-pattern of some object-oriented programming languages. Call super is a design pattern in which a particular class stipulates that in a derived subclass, the user is required to override a method and call back the overridden function itself at a particular point. The overridden method may be intentionally incomplete, and reliant on the overriding method to augment its functionality in a prescribed manner. However, the fact that the language itself may not be able to enforce all conditions prescribed on this call is what makes this an anti-pattern.
Call super is a code smell or anti-pattern of some object-oriented programming languages. Call super is a design pattern in which a particular class stipulates that in a derived subclass, the user is required to override a method and call back the overridden function itself at a particular point. The overridden method may be intentionally incomplete, and reliant on the overriding method to augment its functionality in a prescribed manner. However, the fact that the language itself may not be able to enforce all conditions prescribed on this call is what makes this an anti-pattern. abstract class ReportGenerator { public virtual Report CreateReport() { // Generate the general report object // ... 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 in the store-specific way // ... // Design of this class requires the parent CreateReport() function to be called at the // end of the overridden function. But note this line could easily be left out, or the // returned report could be further modified after the call, violating the class design // and possibly also the company-wide report format. return base.CreateReport(); } } A preferable interface looks like this: abstract class ReportGenerator { public Report CreateReport() { Tabulate(); // Generate the general report object // ... return new Report(...); } protected abstract void Tabulate(); } An implementation would override this class like this: class ConcreteReportGenerator : ReportGenerator { protected override void Tabulate() { // Tabulate data in the store-specific way // ... } }
Text derived from https://en.wikipedia.org/w/index.php?oldid=1309136717 under CC BY-SA 4.0; modifications have been made.