Skip to content
Sharp

Modern Java · interview question

How can you use an enum to implement a strategy pattern for different payment methods in an e-commerce application?

Multiple choiceHardchapter-7-curatedpremiummodern-javaenums
Choose one answer
Answer and explanation

Correct answer

D. Define abstract methods in the enum and override them for each constant.

Why

You can implement the strategy pattern by defining abstract methods within the enum and providing specific implementations for each constant. This allows you to encapsulate different behaviors for each payment method directly within the enum.

Test yourself on Modern Java

A free test with instant, explained results. No account needed.

Start free test
All Modern Java questions →