Skip to main content

How does Factory Method differ from a plain constructor?

Factory Method differs from a plain constructor in that it delegates choosing the specific class and the object-creation process to subclasses or external logic, whereas a constructor always creates a strictly defined object.


1. Control over creation

  • Constructor - hard-fixed: new Car() always creates a Car.

  • Factory Method - lets you decide at runtime which object to create:

    java
    Transport t = createTransport(); // could be a Truck or a Ship

2. Flexibility

  • A constructor cannot be overridden.
  • A factory method can be inherited and have its behavior changed without touching the rest of the code.

3. Encapsulating the creation logic

  • A constructor does not hide the creation process.
  • A factory method can include complex logic: validation, caching, choosing a type based on data.

4. Extensibility

  • Adding a new type with a constructor requires rewriting the client code.
  • With a factory method, it is enough to create a new subclass with the needed implementation.

Summary: A constructor is direct creation of a specific object. Factory Method is a mechanism for choosing and creating a suitable object without tying it to specific classes, which makes the architecture more flexible and scalable.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.