Java Reference
In-Depth Information
Chapter 4. Interfaces
"Conducting" is when you draw "designs" in the nowherewith your
stick, or with your handswhich are interpreted as "instructional mes-
sages" by guys wearing bow ties who wish they were fishing.
Frank Zappa
The fundamental unit of programming in the Java programming language
is the class, but the fundamental unit of object-oriented design is the
type. While classes define types, it is very useful and powerful to be able
to define a type without defining a class. Interfaces define types in an
abstract form as a collection of methods or other types that form the con-
tract for that type. Interfaces contain no implementation and you cannot
create instances of an interface. Rather, classes can expand their own
types by implementing one or more interfaces. An interface is an expres-
sion of pure design, whereas a class is a mix of design and implementa-
tion.
A class can implement the methods of an interface in any way that the
designer of the class chooses. An interface thus has many more possible
implementations than a class. Every major class in an application should
be an implementation of some interface that captures the contract of that
class.
Classes can implement more than one interface. The Java programming
language allows multiple inheritance of interface but only single inherit-
ance of implementationa class can extend only one other class. Classes
can use inheritance of interfaces to expand their type and then use,
for example, composition to provide an implementation for those in-
terfaces. This design allows the typing flexibility of multiple inheritance
while avoiding the pitfalls of multiple implementation inheritance, at the
cost of some additional work for the programmer.
In a given class, the classes that are extended and the interfaces that are
implemented are collectively called the supertypes, and from the view-
point of the supertypes, the new class is a subtype. The new class in-
cludes all its supertypes, so a reference to an object of the subtype can
 
Search WWH ::




Custom Search