Junior — Middle
Is it possible to declare attributes inside an interface in object-oriented programming?
sobes.tech AI
Answer from AI
In Java, interfaces cannot contain regular attributes (instance variables). However, within an interface, you can only declare constants — that is, variables that are by default public static final.
Example:
public interface MyInterface {
int CONSTANT_VALUE = 10; // public static final by default
void doSomething();
}
Thus, in interfaces, you cannot declare mutable fields, only constants. All non-static fields must be in classes that implement the interface.