An interface is used to specify methods that deriving classes must implement. They are
declared with the interface keyword followed by a name and a code block. Their naming convention
is the same as for classes, which is to have each word initially capitalized. When an interface
is not nested inside another containing type their access level is package-private by default,
but they can also be made public just as any other top level member.
The code block for the interface can first of all contain signatures for instance methods.
These methods cannot have any implementations. Instead, their bodies are replaced by semicolons.
Interface members must always be public and since this is the default access level here
we can just as well leave out this modifier. The second member that an interface can contain
is constants. Any field created in an interface will implicitly be declared as static final,
so these modifiers can also be left out. In addition to method signatures and constants,
an interface can also contain nested types - such as classes or other interfaces.
To show an example of how to use an interface we'll create one called Comparable which has
a single method named compare. We'll also create a class called Circle with
a field that holds the radius of the circle. In order to make this class implement the
interface we use the implements keyword after the class name. By convention, the implements
clause is placed after the extends clause if the class has one. Note that although a
class can only inherit from one superclass, it may implement any number of interfaces
by specifying them in a comma separated list.
Because Circle implements Comparable we must now define the Compare method, which for this
class will return the difference between the Circle radiuses. The implemented method must
be public in addition to having the same signature as the method defined in the interface.
Comparable demonstrates the first usage of interfaces, which is to define a specific
functionality that classes can share. It makes it possible to use the interface members without
having to know the actual type of a class. We could now for example create a simple method
that takes two Comparable objects and returns the largest one. This method will work for
all classes that implements the Comparable interface, regardless of their type, since
the method only uses the functionality exposed through that interface.
A second way to use an interface is to provide an actual interface for a class outwards.
In this example we have defined an interface for MyClass called MyInterface. This interface
only includes the functionality that programmers using MyClass may need.
The interface is then used to encapsulate the implementing class so that the class is
only seen through this interface. This abstraction provides two benefits. First,
it makes it easier for other programmers to use our class since they now only have access
to the methods that are relevant. And second, it makes our class more flexible since we
can now implement the class anyway we want as long as we follow the interface and it
won't have any affect on other programmers using our class.
As mentioned before an interface can contain nested types such as classes. In contrast
to methods, these types are implemented inside of the interface. This can for example be
used to provide static helper methods. The types can then be used by classes implementing
the interface. They will not however be visible to other objects using the interface.