These ten questions explain modern interface behavior, inherited contracts, interface fields, and access control.
Answer: It defines a common contract that unrelated classes can implement without sharing a superclass implementation. Client code can depend on the capability rather than a specific concrete class. A class can implement multiple interfaces while extending only one class.
For example, different storage backends can implement one repository interface. This supports substitution and testing, but the interface must still express a meaningful behavioral contract. Matching method signatures alone does not guarantee interchangeable behavior.
Answer: Both can declare abstract methods and provide implementations. An abstract class can hold instance state, define constructors, and use class-member access levels. An interface can provide public default methods, static methods, and private helper methods, but has no per-instance fields or constructors.
The statement that interfaces contain only abstract methods has been obsolete since Java 8. Use an abstract class when shared instance implementation and state are appropriate; use an interface to express a capability across otherwise unrelated classes. Default-method conflicts may require an implementing class to provide an explicit override.
Answer: A method declaration such as String name(); is implicitly public and abstract. The blanket statement is false for all interface methods: default and static methods have bodies, and private instance or private static helpers are allowed.
Private interface methods support implementation reuse inside the interface; they are not inherited by implementing classes. Interface methods cannot be protected or package-private. A class implementing a public abstract interface method must provide a public implementation, rather than reduce its visibility.
Answer: Interface fields are implicitly public, static, and final, even when the keywords are omitted. They require initializers and cannot represent per-instance mutable fields.
Final prevents reassignment, not mutation of a referenced object. Thus an interface field holding a mutable collection can expose shared mutable state, which is usually undesirable. A field is a compile-time constant only when it meets the narrower constant-variable rules, such as a final int initialized by a constant expression.
Answer: Java 8 added default and static interface methods and lambda support for functional interfaces. Java 9 added private interface methods. Java SE 25 also supports sealed interfaces that restrict direct implementations. Generics already existed before Java 8; they were introduced in Java 5.
public class ModernInterface {
@FunctionalInterface
interface Greeting {
String name();
default String message() { return prefix() + name(); }
private String prefix() { return "Hello, "; }
static Greeting of(String name) { return () -> name; }
}
public static void main(String[] args) {
System.out.println(Greeting.of("Ada").message());
}
}
This prints Hello, Ada. The default, private, and static methods do not add abstract obligations, so this interface remains functional. A functional interface has one abstract-method contract after the language's inheritance and Object-method rules are applied.
Answer: An interface uses extends, not implements, to derive from one or more interfaces. A class uses implements to declare interface contracts it satisfies. An interface cannot extend an ordinary class.
For example, interface Combined extends Readable, AutoCloseable {} combines obligations from two interfaces. It does not itself need to provide all implementations. A concrete implementing class must meet the resulting contract, and conflicting inherited methods may require explicit resolution.
Answer: Yes. Interface fields are public, and eligible inherited fields can be referenced by simple name in the implementing class. However, hiding or multiple inherited fields with the same name can make a simple name misleading or ambiguous.
Qualify shared values with their declaring interface name for clarity. Do not implement an interface merely to obtain short names for constants; implementing it declares a type relationship. A static import or explicit qualification can express access to a constant without introducing that relationship.
Answer: No. Every interface field is public static final, but compile-time constant has a narrower meaning. int LIMIT = 10; is a constant variable. A field initialized by a method call, or a reference to a collection, is not necessarily a compile-time constant.
For a reference-valued field, final fixes the reference. It does not freeze the referenced object. Prefer immutable values for public shared fields, and remember that compile-time primitive and String constants may be inlined into separately compiled client code.
Answer: Public, protected, package access, and private. Package access is expressed by omitting an access modifier. Ordinary top-level classes have only public or package access, while member declarations have context-dependent options.
Access is also constrained by the accessibility of the declaring type. In named modules, exports and readability matter for access across module boundaries. Do not confuse access modifiers with final or static: those govern reassignment or membership, not who may use the declaration.
Answer: Accessibility asks whether code at a particular location may refer to a declaration. Inheritance asks which members become members of a subtype under Java's inheritance rules. They interact but are not identical.
A private superclass method is not inherited or overridden, though code within the same enclosing top-level class may have private-access privileges. A public static interface method can be accessible through the interface name without being inherited by implementing classes. Imports only affect name lookup; they do not alter either access or inheritance rules.
References: Interface members and inheritance, Final and constant variables, and Access control.