e01.org

call super

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. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

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 // ... } }

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"call super". 1756796219. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1309136717

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·