Junior — Middle
What elements or methods can be declared in an interface in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming (using Java and Android as examples), interfaces can declare:
- Abstract methods (without implementation), which must be implemented by classes implementing the interface.
- Constants (fields
public static final), i.e., static immutable values.
Starting from Java 8, interfaces can also declare:
- Methods with default implementation (
defaultmethods). - Static methods with implementation.
Example of an interface:
public interface MyInterface {
void abstractMethod(); // abstract method
default void defaultMethod() {
System.out.println("Default implementation");
}
static void staticMethod() {
System.out.println("Interface static method");
}
int CONSTANT = 10; // constant
}