Junior — Middle
What is the difference between variables declared in an interface and variables in an abstract class?
sobes.tech AI
Answer from AI
In Java, variables declared in an interface are by default public static final — that is, constants accessible without creating an instance, and they cannot be changed.
In an abstract class, variables can have any access modifier, not necessarily final or static. They can be instance variables (each object has its own copy) or static. An abstract class allows storing state, whereas an interface only allows constants.
Example:
interface MyInterface {
int CONST = 10; // public static final by default
}
abstract class MyAbstractClass {
protected int value; // instance variable
public static final int CONST = 20; // constant
}