Interface variables Flashcards
Give interface variables rules !
- an interface variable is assumed to be public static and final so
it can not be declared as protected or private or abstract - The value of an interface variable must be set when it is declared since it is final
Give a characteristic of interface variables !
Interface variables are accessible without creating an instance using a reference variable of the type of the interface. They can be accessed used the interface name directly.
Give an example of an interface with all the assumed parts declared !
public interface CanSwim {
int MAXIMUM_DEPTH = 100;
final static boolean UNDERWATER = true;
public static final String TYPE = “Submersible”;
}
public interface CanSwim {
public static final int MAXIMUM_DEPTH = 100;
public static final boolean UNDERWATER = true;
public static final String TYPE = “Submersible”;
}
Give an example that violates all the rules !
public interface CanDig {
private int MAXIMUM_DEPTH = 100; // DOES NOT COMPILE
protected abstract boolean UNDERWATER = false; // DOES NOT COMPILE
public static String TYPE; // DOES NOT COMPILE
}
The first example, MAXIMUM_DEPTH, doesn’t compile because the private modifier is used, and all interface variables are assumed to be public
The second line, UNDERWATER, doesn’t compile for two reasons. It is marked as protected, which conflicts with the assumed modifier public, and it is marked as abstract, which conflicts with the assumed modifier final
Finally, the last example, TYPE, doesn’t compile because it is missing a value