Sobes.tech
Junior — Middle

Is it possible to declare a constructor in an interface?

sobes.tech AI

Answer from AI

In Java, interfaces cannot have constructors because they are not intended for creating instances directly.

An interface is a contract that describes methods that classes must implement.

If a constructor is needed, it is declared in the class that implements the interface.

Starting from Java 8, interfaces can declare methods with implementations (default methods), but constructors are still prohibited.

Example:

interface MyInterface {
    void doSomething();
    // Constructor cannot be declared here
}

class MyClass implements MyInterface {
    public MyClass() {
        // Class constructor
    }

    public void doSomething() {
        // Implementation
    }
}

Thus, constructors are absent in interfaces.

Is it possible to declare a constructor in an… - sobes.tech