Bird
0
0

Which of the following is the correct way to declare a template method in a class in Java?

easy🧠 Conceptual Q3 of 15
LLD - Behavioral Design Patterns — Part 1
Which of the following is the correct way to declare a template method in a class in Java?
Apublic final void templateMethod() { step1(); step2(); }
Bpublic abstract void templateMethod() { step1(); step2(); }
Cprivate void templateMethod() { step1(); step2(); }
Dstatic void templateMethod() { step1(); step2(); }
Step-by-Step Solution
Solution:
  1. Step 1: Understand template method declaration

    Template method is usually final to prevent override and defines algorithm steps.
  2. Step 2: Check options for final method with implementation

    public final void templateMethod() { step1(); step2(); } declares a final method with steps; others are abstract, private, or static which are incorrect.
  3. Final Answer:

    public final void templateMethod() { step1(); step2(); } -> Option A
  4. Quick Check:

    Template method is final with implementation = public final void templateMethod() { step1(); step2(); } [OK]
Quick Trick: Template method is final to fix algorithm steps [OK]
Common Mistakes:
MISTAKES
  • Declaring template method abstract without implementation
  • Making template method private or static
  • Allowing subclasses to override template method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes