System, but they may not be reproduced for publication



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

required if the class needs to return a different ID.)
It is both possible and common for an implementing class to define its own
implementation of a default method. For example, MyIFImp2 overrides
getAdminID( ), as shown here:


Now, when getAdminID( ) is called, a value other than its default is returned.
A More Practical Example of a Default Method
Although the preceding shows the mechanics of using default methods, it doesn’t
illustrate their usefulness in a more practical setting. To do this, let’s return to the
Series interface shown earlier in this chapter. For the sake of discussion, assume that
Series is widely used and many programs rely on it. Further assume that through an
analysis of usage patterns, it was discovered that many implementations of Series
were adding a method that returned an array that contained the next n elements in the
series. Given this situation, you decide to enhance Series so that it includes such a
method, calling the new method getNextArray( ) and declaring it as shown here:
Here, n specifies the number of elements to retrieve. Prior to default methods, adding
this method to Series would have broken preexisting code because existing
implementations would not have defined the method. However, by providing a default
for this new method, it can be added to Series without causing harm. Let’s work
through the process.
In some cases, when a default method is added to an existing interface, its
implementation simply reports an error if an attempt is made to use the default. This
approach is necessary in the case of default methods for which no implementation can
be provided that will work in all cases. These types of default methods define what is,
essentially, optional code. However, in some cases, you can define a default method
that will work in all cases. This is the situation for getNextArray( ). Because Series
already requires that a class implement getNext( ), the default version of
getNextArray( ) can use it. Thus, here is one way to implement the new version of
Series that includes the default getNextArray( ) method:


Pay special attention to the way that the default method getNextArray( ) is
implemented. Because getNext( ) was part of the original specification for Series, any
class that implements Series will provide that method. Thus, it can be used inside
getNextArray( ) to obtain the next n elements in the series. As a result, any class that
implements the enhanced version of Series will be able to use getNextArray( ) as is,
and no class is required to override it. Therefore, no preexisting code is broken. Of
course, it is still possible for a class to provide its own implementation of
getNextArray( ), if you choose.
As the preceding example shows, the default method provides two major benefits:

It gives you a way to gracefully evolve interfaces over time without breaking existing
code.

It provides optional functionality without requiring that a class provide a
placeholder implementation when that functionality is not needed.
In the case of getNextArray( ), the second point is especially important. If an
implementation of Series does not require the capability offered by getNextArray( ),
it need not provide its own placeholder implementation. This allows cleaner code to be
created.
Multiple Inheritance Issues
As explained earlier in this book, Java does not support the multiple inheritance of


classes. Now that an interface can include default methods, you might be wondering if
an interface can provide a way around this restriction. The answer is, essentially, no.
Recall that there is still a key difference between a class and an interface: a class can
maintain state information (through the use of instance variables), but an interface
cannot.
The preceding notwithstanding, default methods do offer a bit of what one would
normally associate with the concept of multiple inheritance. For example, you might
have a class that implements two interfaces. If each of these interfaces provides default
methods, then some behavior is inherited from both. Thus, to a limited extent, default
methods do support multiple inheritance of behavior. As you might guess, in such a
situation, it is possible that a name conflict will occur.
For example, assume that two interfaces called Alpha and Beta are implemented by a
class called MyClass. What happens if both Alpha and Beta provide a method called
reset( ) for which both declare a default implementation? Is the version by Alpha or
the version by Beta used by MyClass? Or, consider a situation in which Beta extends
Alpha. Which version of the default method is used? Or, what if MyClass provides its
own implementation of the method? To handle these and other similar types of
situations, Java defines a set of rules that resolve such conflicts.
First, in all cases a class implementation takes priority over an interface default
implementation. Thus, if MyClass provides an override of the reset( ) default
method, MyClass’s version is used. This is the case even if MyClass implements both
Alpha and Beta. In this case, both defaults are overridden by MyClass’s
implementation.
Second, in cases in which a class inherits two interfaces that both have the same default
method, if the class does not override that method, then an error will result. Continuing
with the example, if MyClass inherits both Alpha and Beta, but does not override
reset( ), then an error will occur.
In cases in which one interface inherits another, with both defining a common default
method, the inheriting interface’s version of the method takes precedence. Therefore,
continuing the example, if Beta extends Alpha, then Beta’s version of reset( ) will be
used.
It is possible to refer explicitly to a default implementation by using a new form of
super. Its general form is shown here:

Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   37   38   39   40   41   42   43   44   ...   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ə