System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə40/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   36   37   38   39   40   41   42   43   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

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 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
implementations 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
interface 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
difference between an interface and a class is that a class can maintain state
information, but an interface cannot. Furthermore, it is still not possible to create an
instance of an interface by itself. It must be implemented by a class. Therefore, even
though, beginning with JDK 8, an interface can define default methods, the interface


must still be implemented by a class if an instance is to be created.
One last point: As a general rule, default methods constitute a special­purpose feature.
Interfaces that you create will still be used primarily to specify what and not how.
However, the inclusion of the default method gives you added flexibility.
Default Method Fundamentals
An interface default method is defined similar to the way a method is defined by a
class. The primary difference is that the declaration is preceded by the keyword
default. For example, consider this simple interface:
MyIF declares two methods. The first, getUserID( ), is a standard interface method
declaration. It defines no implementation whatsoever. The second method is
getAdminID( ), and it does include a default implementation. In this case, it simply
returns 1. Pay special attention to the way getAdminID( ) is declared. Its declaration
is preceded by the default modifier. This syntax can be generalized. To define a default
method, precede its declaration with default.
Because getAdminID( ) includes a default implementation, it is not necessary for an
implementing class to override it. In other words, if an implementing class does not
provide its own implementation, the default is used. For example, the MyIFImp class
shown next is perfectly valid:


The following code creates an instance of MyIFImp and uses it to call both
getUserID( ) and getAdminID( ).
The output is shown here:
As you can see, the default implementation of getAdminID( ) was automatically used.
It was not necessary for MyIFImp to define it. Thus, for getAdminID( ),
implementation by a class is optional. (Of course, its implementation by a class will be

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   36   37   38   39   40   41   42   43   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə