Java Reference
In-Depth Information
13
Interfaces and Inner Classes
Art, it seems to me, should simplify. That, indeed, is very nearly the
whole of the higher artistic process; finding what conventions of form
and what details one can do without and yet preserve the spirit of the
whole....
WILLA SIBERT CATHER,
On the Art of Fiction
Introduction
specifies a set of methods that any class that implements the interface
must have. An interface is itself a type, which allows you to define methods with
parameters of an interface type and then have the code apply to all classes that imple-
ment the interface. One way to view an interface is as an extreme form of an abstract
class. However, as you will see, an interface allows you to do more than an abstract
class allows you to do. Interfaces are Java's way of approximating multiple inheritance.
You cannot have multiple base classes in Java, but interfaces allow you to approximate
the power of multiple base classes.
The second major topic of this chapter is
A Java
interface
An inner class is simply a
class defined within another class. Since inner classes are local to the class that contains
them, they can help make a class self-contained by allowing you to make helping
classes inner classes.
inner classes.
Prerequisites
Section 13.1 on interfaces and Section 13.2 on simple uses of inner classes are inde-
pendent of each other and can be covered in any order. Section 13.3 on more subtle
details of inner classes requires both Sections 13.1 and 13.2.
Section 13.1 on interfaces requires Chapters 1 through 9. No material from Chap-
ters 10 through 12 is used anywhere in this chapter.
Section 13.2 on simple uses of inner classes requires Chapters 1 through 5. It does
not use any material from Chapters 6 through 12.
Section 13.3 on more advanced inner class material requires both Sections 13.1 and
13.2 (and of course their prerequisites). The material in Section 13.3 is not used else-
where in this topic.
Search WWH ::




Custom Search