Java Reference
In-Depth Information
CHAPTER
10
Interfaces
Interfaces are a fundamental feature of the Java language and need to be
understood before we can delve much further into the Java language. This
chapter discusses what an interface is and how it is used, including writing
and implementing an interface, declaring constants in interfaces, extending
interfaces, and the effect of interfaces on polymorphism.
An Overview of Interfaces
An interface is a collection of abstract methods. A class implements an interface,
thereby inheriting the abstract methods of the interface. Unless the class that
implements the interface is abstract, all the methods of the interface need to be
defined in the class. An interface is similar to a class in the following ways:
An interface can contain any number of methods.
■■
An interface is written in a file with a .java extension, with the name of
the interface matching the name of the file.
■■
The bytecode of an interface appears in a .class file.
■■
Interfaces appear in packages, and their corresponding bytecode file
must be in a directory structure that matches the package name.
■■
295
Search WWH ::




Custom Search