Java Reference
In-Depth Information
Figure 9.3
Output of FancyPicture
9.5 Interfaces
Inheritance is a very useful tool because it enables polymorphism and code sharing,
but it does have several limitations. Because Java uses single inheritance, a class can
extend only one superclass. This makes it impossible to use inheritance to set up mul-
tiple is-a relationships for classes that share multiple characteristics, such as an
employee who is both part-time and a secretary. (Some languages, such as C++, do
allow multiple inheritance, but this can be complicated and cause subtle problems, so
the designers of Java left it out of the language.) There are also situations in which
you want is-a relationships and polymorphism but you don't want to share code, in
which case inheritance isn't the right tool for the job.
To this end, Java provides a feature called an interface that can represent a
common supertype between several classes without code sharing.
Interface
A type that consists of a set of method declarations; when classes promise
to implement an interface, you can treat those classes similarly in your
code.
 
 
Search WWH ::




Custom Search