5.3. Declaring an Interface¶
Interfaces, just like classes, have a fully qualified name. Their
source code should be positioned and named within your project the
same as with classes. That is, an interface called
cs1302.interfaces.contract.Styleable
has an implied position
within the package directories of your source code
(cs1302/interfaces/contract
) and should be placed in a
Styleable.java
file within the implied directory. The first big
syntax difference between a class and an interface is illustrated in
the first line of Styleable.java
that looks like this:
public interface Styleable {
When declaring an interface, we use the interface
keyword instead
of class
in the type declaration near the top of the file.
Note
Up until this point, you’ve probably used the term “class declaration”
for this type of declaration, which is appropriate when creating a
class. However, we say “type declaration” as a more general term as
a .java
file can be used to declare any kind of reference type in
Java, including classes, interfaces, and enumerations.
Remember Reference Types
Remember, a reference type in Java is any type that can serve as the type for a variable that refers to an object. Such a variable is known as a reference variable. We will elaborate on this terminology in the context of interfaces a little more later in this tutorial. If you are unfamiliar with these terms in general, please review the Reference Variables Chapter. You are encouraged to ask questions about any parts that you find confusing.
The second big syntax difference between classes and interfaces involves
the inclusion of abstract methods in the latter. You can see an example
of this in illustrated by the style()
method in Styleable.java
:
public void style();
Notice that the style()
method does not contain an
implementation! The signature of the method ends with a semicolon. An
abstract method must not have an implementation.
Test Yourself
Is the following an abstract method?
public void style() { }
Test Yourself Solution (Try the above question first)
No, it is not an abstract method.
While the { }
may not do anything, it is, in fact, an
implementation that does nothing. Compare that to the actual abstract
method signature presented above that ends with a semicolon, thus
lacking an implementation.
If you open the Styleable.java
file, you will see that it also
contains an abstract unstyle
method. Remember, that the abstract
method(s) represent what the signer of the contract must be able to
do. If a class implements the Styleable
interface, it is
obligated to have a concrete (non-abstract) style
and a concrete
unstyle
method. If an implementing class does not have
implementations for one or both of these methods, it will not
compile.
Note
In Java, the declaration of an abstract method in the
source code for an interface may omit the public
visibility
modifier. If public
is omitted in this context, the abstract
method is still assumed to have public
visibility. This behavior
is different for classes, a topic that will be covered more in-depth
at a later time when the nuances of visibility are presented.
Learn by Practicing!
Generate, host, and view the API documentation website for the
starter code provided with this tutorial. Find the Styleable
interface on the website and compare the documentation provided there
with what you see in the Javadoc comments included in
Styleable.java
. If you don’t remember how to do this, then please
refer back to your notes for the Javadoc and API
Documentation
tutorial.