Difficulty: Easy
Correct Answer: Because an interface defines a public contract that implementing classes must fulfill, all its methods must be accessible to those classes and therefore are implicitly public.
Explanation:
Introduction / Context:
Interfaces in Java are designed to define contracts that other classes agree to implement. A key part of that design is the access level of interface methods. This question explores why interface methods are always effectively public and how that supports the idea of a shared contract between unrelated types.
Given Data / Assumptions:
Concept / Approach:
The main purpose of an interface is to define a set of methods that client code can call on any implementing object, regardless of the specific class. To achieve this, methods in the interface must be visible everywhere that the interface itself is visible. If the interface is public, and some methods were private or protected, implementing classes in other packages would not be able to call them or even see them, which would break the idea of a consistent contract. For this reason, standard interface methods are implicitly public and abstract, ensuring that all implementing classes can use and implement them consistently.
Step-by-Step Solution:
Step 1: Recognize that when you write interface Runnable { void run(); } the method run is implicitly public and abstract.Step 2: An implementing class such as class MyTask implements Runnable must provide a public void run() implementation, because it is implementing a public contract.Step 3: If interface methods could be private or protected, code outside the interface package might not be able to call them, which would make the interface less useful as a general contract.Step 4: The Java language designers therefore specified that interface methods are public so that any code that has access to the interface type can safely call its declared methods.Step 5: Option A correctly captures this reasoning about interfaces defining public contracts that require public method access.
Verification / Alternative check:
To verify, write an interface with a method that omits the public keyword and then compile an implementing class. The compiler requires the implementing method to be declared public, confirming that the interface method was treated as public. Attempting to use a weaker access modifier such as protected or package private for the implementation causes a compilation error, which reinforces that the contract is public by design.
Why Other Options Are Wrong:
Option B claims the compiler cannot handle other modifiers, which is not accurate; modern Java supports private methods in interfaces, but only under specific rules and with bodies. Option C incorrectly states that interface methods are package private by default, which contradicts the specification. Option D suggests that interfaces are deprecated and only kept for compatibility, which is not true since they remain heavily used in modern Java applications and frameworks.
Common Pitfalls:
A common misconception is that adding public to interface methods is required, when in fact it is redundant but often kept for clarity. Another pitfall is assuming that an implementing method can use a more restrictive access modifier, which violates the rule that you cannot weaken access when overriding. Remember that interface methods represent capabilities that must be visible wherever the interface is used, so public access is both natural and necessary.
Final Answer:
Interface methods are always effectively public because an interface defines a public contract that implementing classes must fulfill, so its methods must be accessible wherever the interface is visible.
Discussion & Comments