Java Reference
In-Depth Information
intended. The @Override annotation on interface method implementations ensure such errors are flagged in
an abstract class.
Extending Interfaces
You can define one interface based on another by using the keyword extends to identify the base interface
name. This is essentially the same form as you use to derive one class from another. The interface doing the
extending acquires all the methods and constants from the interface it extends. For example, the interface
Conversions would perhaps be more useful if it contained the constants that the original interface Conver-
sionFactors contained. This would obviate the need for a separate class containing the constants, so there
would be no need for the static import statement.
You could do this by defining the interface Conversions as follows:
public interface Conversions extends ConversionFactors {
double inchesToMillimeters(double inches);
double ouncesToGrams(double ounces);
double poundsToGrams(double pounds);
double hpToWatts(double hp);
double wattsToHP(double watts);
}
Now the interface Conversions also contains the members of the interface ConversionFactors . Any
class implementing the Conversions interface has the constants from ConversionFactors available to im-
plement the methods. Analogous to the idea of a superclass, the interface ConversionFactors is referred to
as a super-interface of the interface Conversions .
Of course, because the constants and the methods involved in conversion operations are closely related,
it would have been much better to put them all in a single interface definition. But then it wouldn't demon-
strate one interface extending another.
Interfaces and Multiple Inheritance
Unlike a class, which can extend only one other class, an interface can extend any number of other inter-
faces. To define an interface that inherits the members of several other interfaces, you specify the names of
the interfaces separated by commas following the keyword extends . For example:
public interface MyInterface extends HisInterface, HerInterface {
// Interface members - constants and abstract methods...
}
Now MyInterface inherits all the methods and constants that are members of HisInterface and Her-
Interface . This is described as multiple inheritance . In Java, classes do not support multiple inheritance,
only interfaces do.
Some care is necessary when you use this capability. If two or more super-interfaces declare a method
with the same signature — that is, with identical names and parameters — the method must have the same
return type in all the interfaces that declare it. If they don't, the compiler reports an error. This is because it
would be impossible for a class to implement both methods, as they have the same signature. If the method
is declared identically in all the interfaces that declare it, then a single definition in the class satisfies all the
interfaces. As I said in the previous chapter, every method in a class must have a unique signature, and the
return type is not part of it.
Search WWH ::




Custom Search