Java Reference
In-Depth Information
As an experiment, you might try removing the implementation for meth1( ) in MyClass .
This will cause a compile-time error. As stated earlier, any class that implements an inter-
face must implement all methods required by that interface, including any that are inherited
from other interfaces.
Default Interface Methods
As explained earlier, prior to JDK 8, an interface could not define any implementation
whatsoever. This meant that for all previous versions of Java, the methods specified by
an interface were abstract, containing no body. This is the traditional form of an interface
and is the type of interface that the preceding discussions have used. The release of JDK 8
changed this by adding a new capability to interface called the default method . A default
method lets you define a default implementation for an interface method. In other words,
by use of a default method, it is now possible for an interface method to provide a body,
rather than being abstract. During its development, the default method was also referred to
as an extension method , and you will likely see both terms used.
A primary motivation for the default method was to provide a means by which interfaces
could be expanded without breaking existing code. Recall that there must be implementa-
tions for all methods defined by an interface. In the past, if a new method were added to a
popular, widely used interface, then the addition of that method would break existing code
because no implementation would be found for that method. The default method solves this
problem by supplying an implementation that will be used if no other implementation is
explicitly provided. Thus, the addition of a default method will not cause preexisting code
to break.
Another motivation for the default method was the desire to specify methods in an in-
terface that are, essentially, optional, depending on how the interface is used. For example,
an interface might define a group of methods that act on a sequence of elements. One of
these methods might be called remove( ) , and its purpose is to remove an element from
the sequence. However, if the interface is intended to support both modifiable and non-
modifiable sequences, then remove( ) is essentially optional because it won't be used by
non-modifiable sequences. In the past, a class that implemented a non-modifiable sequence
would have had to define an empty implementation of remove( ) , even though it was not
needed. Today, a default implementation for remove( ) can be specified in the interface
that either does nothing or reports an error. Providing this default prevents a class used for
non-modifiable sequences from having to define its own, placeholder version of remove(
) . Thus, by providing a default, the interface makes the implementation of remove( ) by a
class optional.
It is important to point out that the addition of default methods does not change a key
aspect of interface : an interface still cannot have instance variables. Thus, the defining dif-
Search WWH ::




Custom Search