Java Reference
In-Depth Information
Notice that the addition of
getPrevious( )
required a change to the implementations of the
methods defined by
Series
. However, since the interface to those methods stays the same,
the change is seamless and does not break preexisting code. This is one of the advantages
of interfaces.
As explained, any number of classes can implement an
interface
. For example, here is a
class called
ByThrees
that generates a series that consists of multiples of three:
One more point: If a class includes an interface but does not fully implement the methods
defined by that interface, then that class must be declared as
abstract
. No objects of such
a class can be created, but it can be used as an abstract superclass, allowing subclasses to
provide the complete implementation.
Using Interface References
You might be somewhat surprised to learn that you can declare a reference variable of an
interface type. In other words, you can create an interface reference variable. Such a vari-