Junior — Middle
Is it possible to declare attributes inside an interface in an object-oriented program?
sobes.tech AI
Answer from AI
In Java, interfaces can contain only constants (public static final fields) and abstract methods (before Java 8). Starting with Java 8, interfaces can declare methods with implementations (default and static methods), but regular non-static attributes (instance variables) cannot be declared.
Example of a constant in an interface:
public interface MyInterface {
int CONSTANT_VALUE = 10; // public static final by default
void doSomething();
}
Thus, regular attributes inside an interface cannot be declared, only constants.