Junior — Middle
Is it possible to define a constructor inside an interface?
sobes.tech AI
Answer from AI
In Java, interfaces cannot contain constructors because an interface is an abstract type that only defines method signatures and constants, but does not implement object creation logic.
Objects are created through classes that implement the interface, and constructors are defined within these classes.
Example:
interface MyInterface {
void doSomething();
}
class MyClass implements MyInterface {
public MyClass() {
// Constructor of the class
}
public void doSomething() {
System.out.println("Method execution");
}
}
Therefore, it is not possible to define a constructor inside an interface.