Java Reference
In-Depth Information
Interfaces and Multiple Inheritance
Unlike a class, which can only extend one other class, an interface can extend any number of other
interfaces. 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 will inherit all the methods and constants that are members of HisInterface
and HerInterface . 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 will report 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 will satisfy all the interfaces. As we said in the previous chapter, every method in a class must have
a unique signature, and the return type is not part of it.
Using Interfaces
What you have seen up to now has primarily illustrated the mechanics of creating an interface and
incorporating it into a class. The really interesting question is - what should you use interfaces for?
We have already illustrated one use for interfaces. An interface is a very handy means of packaging up
constants. You can use an interface containing constants in any number of different classes that have access to
the interface. All you need to do is make sure the class implements the interface and all the constants it
contains are available. The constants are static and so will be shared among all objects of a class.
An interface that declares methods defines a standard set of operations. Different classes can add such a
standard interface by implementing it. Thus objects of a number of different class types can share a
common set of operations. Of course, a given operation in one class may be implemented quite
differently in one class compared to another. But the way in which you invoke the operation is the same
for objects of all class types that implement the interface. For this reason it is often said that an interface
defines a contract for a set of operations.
We hinted at the third and perhaps most important use of interfaces at the beginning of this discussion.
An interface defines a type, so you can expedite polymorphism across a set of classes that implement
the same interface. This is an extremely useful and powerful facility. Let's have a look at how this works.
Interfaces and Polymorphism
You can't create objects of an interface type but you can create a variable of an interface type. For example:
Conversions converter = null; // Variable of the Conversions
interface type
Search WWH ::




Custom Search