Java Reference
In-Depth Information
abstract interface Updatable {
}
public abstract interface ReadOnly {
}
■
Interfaces in Java are implicitly abstract. Using the keyword
abstract
in their declarations is obsolete and it
should not be used in new programs. the above examples are only for illustration purpose.
Note
Declaring Interface Members
An interface can have three types of members:
•
Constant fields
•
Abstract, static, and default methods
•
Static types (nested interfaces and classes)
Note that an interface declaration is much like a class declaration, except that an interface cannot have mutable
instance and class variables. Unlike a class, an interface cannot be instantiated. All members of an interface are
implicitly public.
Constant Fields Declarations
You can declare constant fields in an interface as shown in Listing 17-7. It declares an interface named
Choices
,
which has declarations of two fields:
YES
and
NO
. Both are of
int
data type.
Listing 17-7.
Declaring Fields in an Interface
// Choices.java
package com.jdojo.interfaces;
public interface Choices {
public static final int YES = 1;
public static final int NO = 2;
}
All fields in an interface are implicitly
public
,
static,
and
final
. Although the interface declaration syntax permits
the use of these keywords in a field declaration, their use is redundant. It is recommended not to use these keywords
when declaring fields in an interface. The
Choices
interface can be declared as follows without changing its meaning:
public interface Choices {
int YES = 1;
int NO = 2;
}
You can access the fields in an interface using the dot notation in the form of
<interface-name>.<field-name>