05 / 13

Can an Interface have a Constructor?

Difficulty: 4/10
interface, constructor, OOP design

Constructors and Interfaces

No, an interface does not have a constructor in Java or C#. A constructor initializes an instance of a concrete class, while an interface is a contract and cannot normally be instantiated directly. The implementing class owns object construction and is responsible for initializing its state. This distinction reinforces the separation between an abstraction's contract and the concrete object's lifecycle.

javascript
  1. 1

    Interfaces cannot normally be instantiated.

  2. 2

    Therefore, they do not have object constructors.

  3. 3

    Implementing classes define constructors.

  4. 4

    Dependency initialization can be performed in the implementing class.

  5. 5

    Interface default/static methods do not change the basic role of constructors.

Scenario Questions

0-2 years experience

  1. 1You need to create a mock implementation of an interface for a unit test. How would you handle any required initialization if the interface can't have a constructor?
  2. 2If you write `new IMyService()` where `IMyService` is an interface, what compile error do you see and why?
  3. 3Suppose an interface has a static method that needs to set up some state. Can you use a constructor for that? Explain.

2-5 years experience

  1. 1During a code review you notice a class trying to call `super()` inside an interface implementation. Why does this fail, and how would you fix it?
  2. 2A teammate added a default method that needs a configuration value and tried to initialize it in a constructor inside the interface. Explain why it doesn't work and propose an alternative.
  3. 3Your application crashes at startup with a `NoSuchMethodError` related to an interface. Walk me through how you would debug the issue.

5-8 years experience

  1. 1We have dozens of services implementing `IRepository`. They all need to establish a DB connection during construction. Discuss the trade‑offs of using an abstract base class with a constructor versus keeping `IRepository` as a pure interface.
  2. 2If we wanted to enforce that every implementation of `ILogger` provides a timestamp field, could we use an interface constructor? If not, how would you design the system to guarantee this across teams?
  3. 3Explain how you would refactor a legacy codebase that placed initialization logic in static fields of an interface to improve testability and maintainability.

8+ years experience

  1. 1Our platform spans multiple microservices written in Java and Kotlin. Some services use interfaces with static initialization blocks for shared config, leading to classloader issues. How would you redesign the contract to avoid constructors while ensuring consistent initialization across services?
  2. 2We are migrating from Java 8 to Java 17 and want to introduce default methods that need per‑implementation state. Discuss the architectural implications of not having constructors in interfaces and how you would manage state at scale.
  3. 3From a long‑term maintenance perspective, when would you choose an abstract class over an interface given the inability to define constructors, and how would you communicate that decision across teams?

Follow-up Questions

  • What alternatives exist for providing common initialization logic across implementations?
  • Can a static block in an interface be used to achieve similar effects to a constructor?
  • How would you decide between an abstract class and an interface when you need shared state?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.