Implementing Factory Method in Java
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Factory Method is implemented with inheritance.
You start with an abstract creator class that has an abstract factory method returning an interface type. Then you write a template method in the parent that calls the factory method to get an object and does the shared work with it — and crucially, the parent depends only on the interface, not any concrete class.
Finally, you write subclasses that override the factory method to return specific implementations. The structure means the parent owns the workflow without knowing the exact types, and subclasses supply the types. Let me make it concrete with a report service.
You define an abstract ReportService with an abstract method createGenerator that returns a ReportGenerator interface. Then you write an export method in the parent that calls createGenerator, then calls generate on whatever it gets back. The parent doesn't know if it's a PDF or Excel generator.
Now you create subclasses, PdfReportService and ExcelReportService, and each overrides createGenerator to return its specific generator. The export method is identical for both, but it produces different reports because the factory method is overridden. The mistake freshers make is confusing this with Simple Factory.
Factory Method needs inheritance and an overridden method — a static helper is Simple Factory. The other mistake is having no template method; if you override the creation method but nothing in the parent actually calls it, you've lost the whole benefit.
The key difference to call out is this: with Simple Factory you edit an existing switch to add a type, but with Factory Method you add a new subclass and never touch existing code. That's why Factory Method is the Gang of Four pattern — it follows Open Closed and inverts control.
Implementing Factory Method in Java
Direct Answer
Define an abstract creator with an abstract factory method returning an interface, plus a template method that uses it. Each concrete subclass overrides the factory method to return a specific product. The parent owns the workflow and depends only on the interface; subclasses supply the type. Adding a type = adding a subclass, not editing the parent.
Common Mistakes & Pitfalls
- Confusing it with Simple Factory. A static helper isn't Factory Method — that needs inheritance and an overridden method.
- No template method. Overriding the factory method but having no parent method that uses it loses the inversion of control that gives the pattern its power.
- Returning a concrete type from the factory method — return the interface so subclasses can plug in any implementation.
- Over-engineering — for two types with no shared workflow, Simple Factory is plenty.
Check your understanding
Answer these without looking back at the article.
- What is the product interface role in Factory Method pattern?
- How does Factory Method differ from Abstract Factory in terms of structure?
- Why must the factory method return an interface rather than a concrete type?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.