next up previous
Next: 1.7.1 Multiple Inheritance Up: 1. From C++ to Previous: 1.6 Using Classes to

1.7 Interfaces

We have not yet discussed one of most important object-oriented features of Java, namely the notion of an interface. In essence, an interface is a special lightweight form of abstract class. We use the term lightweight to describe an abstract class with no fields and no concrete methods; the only members are abstract methods. The key difference between an interface and the corresponding abstract class is that a class or interface can have unlimited number of immediate superinterfaces but it can only have one superclass.

An interface definition has almost the same syntax as a class definition:

interface name {

... member declarations ...
}
The only significant difference is the keyword interface instead of class.

In this monograph, we will usually follow the convention that interface names end with a capital``I''. Hence, ``ListI'' is the name of an interface, while ``List'' is the name of a class. There is no generally accepted convention for distinguishing interface names from class names in the Java programming culture. In the Java libraries, for example, there is no way to tell a class from an interface based on its name.

A class can implement an arbitrary number of interfaces. A class definition can optionally include an implements clause immediately following the extends clause (assuming one is present) in the class header. Sample programs using interfaces appear in 1.8.



Subsections
next up previous
Next: 1.7.1 Multiple Inheritance Up: 1. From C++ to Previous: 1.6 Using Classes to
Corky Cartwright 2003-07-07