Sub topic 10
Interface Constants and Implicit Modifiers
In earlier chapters you declared interfaces with methods and no bodies. An interface can also hold values that never change. Java also adds some keywords to interface members on its own, even when you leave them out. This chapter shows what those keywords are and why they matter.
What Is an Interface Constant?
A constant is a value that is set once and can never be changed. In a class, you make a constant by writing public static final. Here is what each word does:
publicmeans any code can see it.staticmeans it belongs to the type itself, not to each object.finalmeans it cannot be given a new value.
In an interface, every field is a constant automatically. You can write int MAX_USERS = 100; and Java treats it as public static final int MAX_USERS = 100;. You must give the field a value when you declare it, because it can never be set later.
What Is an Implicit Modifier?
A modifier is a keyword such as public or abstract that changes how a member behaves. An implicit modifier is one that Java adds for you, even if you do not type it.
| Member in an interface | What you write | What Java treats it as |
|---|---|---|
| Field | `int MAX = 10;` | `public static final int MAX = 10;` |
| Method with no body | `void start();` | `public abstract void start();` |
Default, static, and private methods follow their own rules. Later chapters cover them.
A Worked Example
interface Config {
int MAX_USERS = 100; // really: public static final
String APP_NAME = "Notes"; // really: public static final
void start(); // really: public abstract
}
class App implements Config {
@Override
public void start() {
System.out.println(APP_NAME + " allows " + MAX_USERS + " users");
}
}
public class Main {
public static void main(String[] args) {
new App().start(); // prints: Notes allows 100 users
System.out.println(Config.MAX_USERS); // prints: 100
// Config.MAX_USERS = 5; // compile error: cannot assign a final variable
}
}Notice two things:
AppusesAPP_NAMEandMAX_USERSwithout a prefix because it implementsConfig. Other code reaches them through the interface name, as inConfig.MAX_USERS.start()inAppis markedpublic. That is required, as the next section explains.
The Most Common Mistake
Interface methods are implicitly public. When you override a method, you cannot make it less visible. If you leave out public in the class, the method gets package-private access, which is narrower than public. The compiler rejects it:
class BrokenApp implements Config {
void start() { } // error: attempting to assign weaker access privileges
}Exercise
Given interface Limits { int MAX = 5; void check(); }, a class writes class Gate implements Limits { void check() {} } and later runs Limits.MAX = 6;. Name both compile errors and explain each one.
Show answer
First, check() in Gate has package-private access, but the interface method is implicitly public, so the class tries to weaken access. Fix it by declaring public void check(). Second, MAX is implicitly public static final, so it cannot be given a new value, and Limits.MAX = 6; fails.
Exercise
Write the full form of the declaration double RATE = 0.05; inside an interface, with every implicit modifier shown.
Show answer
public static final double RATE = 0.05;