System, but they may not be reproduced for publication



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

InterfaceName.super.methodName( )


For example, if Beta wants to refer to Alpha’s default for reset( ), it can use this
statement:
USE STATIC METHODS IN AN INTERFACE
JDK 8 added another new capability to interface: the ability to define one or more
static methods. Like static methods in a class, a static method defined by an interface
can be called independently of any object. Thus, no implementation of the interface is
necessary, and no instance of the interface is required in order to call a static method.
Instead, a static method is called by specifying the interface name, followed by a
period, followed by the method name. Here is the general form:
InterfaceName.staticMethodName
Notice that this is similar to the way that a static method in a class is called.
The following shows an example of a static method in an interface by adding one to
MyIF, shown earlier. The static method is getUniversalID( ). It returns zero.
The getUniversalID( ) method can be called, as shown here:
As mentioned, no implementation or instance of MyIF is required to call


getUniversalID( ) because it is static.
One last point: static interface methods are not inherited by either an implementing
class or a subinterface.
PRIVATE INTERFACE METHODS
Beginning with JDK 9, an interface can include a private method. A private interface
method can be called only by a default method or another private method defined by
the same interface. Because a private interface method is specified private, it cannot
be used by code outside the interface in which it is defined. This restriction includes
subinterfaces because a private interface method is not inherited by a subinterface.
The key benefit of a private interface method is that it lets two or more default methods
use a common piece of code, thus avoiding code duplication. For example, here is a
further enhanced version of the Series interface that adds a second default method
called skipAndGetNextArray( ). It skips a specified number of elements and then
returns an array that contains the subsequent elements. It uses a private method called
getArray( ) to obtain an element array of a specified size.


Notice that both getNextArray( ) and skipAndGetNextArray( ) use the private
getArray( ) method to obtain the array to return. This prevents both methods from
having to duplicate the same code sequence. Keep in mind that because getArray( ) is
private, it cannot be called by code outside Series. Thus, its use is limited to the default
methods inside Series.
Although the private interface method is a feature that you will seldom need, in those
cases in which you do need it, you will find it quite useful.
FINAL THOUGHTS ON PACKAGES AND
INTERFACES
Although the examples we’ve included in this book do not make frequent use of


packages or interfaces, both of these tools are an important part of the Java
programming environment. Virtually all real programs that you write in Java will be
contained within packages. A number will probably implement interfaces as well. As
you will see in 
Chapter 15
, packages play an important role in the modules feature. It is
important, therefore, that you be comfortable with their usage.
 Chapter 8 Self Test
1.
Using the code from 
Try This 8­1
, put the ICharQ interface and its three
implementations into a package called qpack. Keeping the queue demonstration class
IQDemo in the default package, show how to import and use the classes in qpack.
2.
What is a namespace? Why is it important that Java allows you to partition the
namespace?
3.
Typically, packages are stored in ______________.
4.
Explain the difference between protected and default access.
5.
Explain the two ways that the members of a package can be used by other packages.
6.
“One interface, multiple methods” is a key tenet of Java. What feature best
exemplifies it?
7.
How many classes can implement an interface? How many interfaces can a class
implement?
8.
Can interfaces be extended?
9.
Create an interface for the Vehicle class from 
Chapter 7
. Call the interface
IVehicle.
10.
Variables declared in an interface are implicitly static and final. Can they be
shared with other parts of a program?
11.
A package is, in essence, a container for classes. True or False?
12.
What standard Java package is automatically imported into a program?
13.
What keyword is used to declare a default interface method?


14.
Beginning with JDK 8, is it possible to define a static method in an interface?
15.
Assume that the ICharQ interface shown in 
Try This 8­1
 has been in widespread
use for several years. Now, you want to add a method to it called reset( ), which will be
used to reset the queue to its empty, starting condition. Assuming JDK 8 or later, how
can this be accomplished without breaking preexisting code?
16.
How is a static method in an interface called?
17.
Can an interface have a private method?


Chapter 9
Exception Handling
Key Skills & Concepts

Know the exception hierarchy

Use try and catch

Understand the effects of an uncaught exception

Use multiple catch statements

Catch subclass exceptions
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out


T

Nest try blocks

Throw an exception

Know the members of Throwable

Use finally

Use throws

Know Java’s built­in exceptions

Create custom exception classes
his chapter discusses exception handling. An exception is an error that occurs at run
time. Using Java’s exception handling subsystem you can, in a structured and
controlled manner, handle run­time errors. Although most modern programming
languages offer some form of exception handling, Java’s support for it is both easy­to­
use and flexible.
A principal advantage of exception handling is that it automates much of the error
handling code that previously had to be entered “by hand” into any large program. For
example, in some older computer languages, error codes are returned when a method
fails, and these values must be checked manually, each time the method is called. This
approach is both tedious and error­prone. Exception handling streamlines error
handling by allowing your program to define a block of code, called an exception

Yüklə 83 Mb.

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