intermediateTemplate Method Pattern

What's the difference between an abstract method and a hook method in the Template Method pattern?

An abstract method has no implementation on the base class at all, and the language forces every concrete subclass to provide one before it can even be instantiated — this is used for steps that are mandatory and genuinely different per subclass, like the actual parsing logic in a file parser. A hook method has a real, usually minimal or empty, default implementation on the base class, and a subclass is free to override it if it needs different behavior, but isn't required to. Hooks are the right tool for optional steps — an afterParse() cleanup step that only some formats need, for instance — where forcing every subclass to implement something would just mean most of them writing an empty method body out of obligation.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to Why does an iterator's current position live on the iterator object itself, rather than on the collection being traversed?← Back to all Low-Level Design & Design Patterns questions