Interface variables Flashcards

1
Q

Give interface variables rules !

A
  1. an interface variable is assumed to be public static and final so
    it can not be declared as protected or private or abstract
  2. The value of an interface variable must be set when it is declared since it is final
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give a characteristic of interface variables !

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give an example of an interface with all the assumed parts declared !

A

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”;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example that violates all the rules !

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly