- Interfaces are in the language mainly to provide much of the functionality
of multiple inheritance.
- A class can inherit from multiple superclasses.
- Problems arise when there are name clashes in the base classes.
- For example, Class FlyingMachine may have a method called navigate()
which does navigation in three dimensions. Class FloatingVessel may have a method with the same name, but which only navigates in two dimensions.
- Seaplane inherits both methods, which immediately results in a name
clash error.
- You don't want to be forced to change the names in one base class because of names in some other base class.
- Even worse, there is no general way to say which of the two navigate()
methods Seaplane should have and which it should forget.
- Interfaces avoid this kind of problem.
- An interface is a skeleton of a class, showing the methods the class will
have when someone implements it.
- An interface may look like this:
interface FlyingMachine {
int navigate(Point from, Point to);
void land();
void takeoff(double fuel);
}
- The declarations in an interface are always public, even if you don't label
them so.
- You can declare data in an interface, but only constant data. Even if
you don't label it "final", this is assumed for you.
- An interface is a way of saying "you need to plug some code in here
for this thing to fully work."
- The interface specifies the exact signatures of the methods that must be
provided.
- A later class implements the interface by defining those methods
including their bodies.
- If two interfaces should happen to demand a method of the same name in a
class, it isn't a problem. It merely says that both interfaces are making the
same demand for a method of that name in the class.
- Here is an example implementation of the FlyingMachine interface:
class helicopter implements FlyingMachine {
double fueltank;
int engine_rpm;
int rotors;
int navigate(Point from, Point to) {
//full body of code appears here
}
void land() {
for( ; engine_rpm>0; engine_rpm--);
}
void takeoff(double fuel) {
fueltank += fuel;
for( ; engine_rpm<6000; engine_rpm++);
}
void hover() {
//full body of code appears here
}
//other methods can be in the class too
}
- Several different classes can implement the same interface, each in their
own way.
- A class may implement several interfaces at once. The FloatingVessel
interface may look like:
interface FloatingVessel {
int navigate(Point from, Point to);
void dropAnchor();
void weighAnchor()
}
- The class Seaplane may then implement the methods promised by both the
FloatingVessel and the FlyingMachine interfaces, like this:
class Seaplane implements FloatingVessel, FlyingMachine {
double fueltank;
int engine_rpm;
int anchorline;
void dropAnchor() {
anchorline = 200;
}
void weighAnchor() {
anchorline = 0;
}
int navigate(Point from, Point to) {
//full body of code appears here
}
void land() {
for ( ; engine_rpm>0; engine_rpm--) ;
dropAnchor();
}
void takeoff(double fuel) {
weighAnchor();
fueltank += fuel;
for( ; engine_rpm<6000; engine_rpm++) ;
}
//other methods can be in the class too
}
- An interface is used to specify the form that something must
have.
- So you can see that an interface is a little like an abstract class, that
must be extended in exactly the manner that its methods present.
- Generally, an interface differs from an abstract class in the following
ways:
- An abstract class is an incomplete class. An interface is a specification
for a prescription of behavior.
- A class can implement several interfaces at once, whereas a class can only
extend one parent class.
- An interface doesn't have any overtones of specialization that are present
with inheritance. It merely says "well, we need something that does 'foo' and
here are the ways that users should be able to call it."
- Essentially, you'll probably use interfaces a lot more than abstract
classes.
- Use an abstract class when you want to initiate a hierarchy of more
specialized classes.
- Use an interface when you just want to say "I want to be able to call
methods with these signatures in your class."