Template Method & Final Keyword

ayushmaan_jain
Mausschubser
Mausschubser
Beiträge: 64
Registriert: 18. Okt 2014 11:11

Template Method & Final Keyword

Beitrag von ayushmaan_jain »

Hi All,

Is it mandatory for a template method to be final ?
Please explain.



Regards,
SJ

ddimitrov
Erstie
Erstie
Beiträge: 20
Registriert: 17. Jul 2015 16:28

Re: Template Method & Final Keyword

Beitrag von ddimitrov »

A template method is always a concrete method (final in Java, or sealed in .NET), so that it cannot be overridden. The reason is to have a full control of it.

ayushmaan_jain
Mausschubser
Mausschubser
Beiträge: 64
Registriert: 18. Okt 2014 11:11

Re: Template Method & Final Keyword

Beitrag von ayushmaan_jain »

If that is the case, then why template method i.e. "append()" of Abstract class in Log4J framework was not marked "final" by its designers.
Dateianhänge
AppenderSkeleton - Log4J
AppenderSkeleton - Log4J
rsz_appenderskeleton__apache_log4j_1217_api_.png (78.59 KiB) 1368 mal betrachtet

ddimitrov
Erstie
Erstie
Beiträge: 20
Registriert: 17. Jul 2015 16:28

Re: Template Method & Final Keyword

Beitrag von ddimitrov »

append() in this case is not the template method, but the doAppend(), which is a concrete method (not abstract). This is ok. Let`s say that the template method is ALWAYS a concrete method. Final would be nice, but is optional.

Antworten

Zurück zu „Archiv“