Thursday, February 17, 2011

What are interfaces?

As it was seen methods (behaviors) in objects define their interaction with the outside world. These method's form the interface which as well can interact with the outside world. Imagine the following scenario: You have been using the previous bike object defined before and you are very familiar with how to speedUp, changeGear, changeCadence, applyBrakes and so on. However, your bike is kind of old and you got a brand new bike. But, it turns out that even when the new bike speeds up, chages gear and applies breaks these methods are named different, so when you try to call the "applyBrakes" method you are used to your new bike simply won't respond because the method name was "bikeBrake", and after that things start getting very ugly.

Interfaces are special contracts that state which operations are performed and how to interact with certain objects. The remote control of your TV is the interface between you and the TV; you just press the turn on button and the TV turns on regardless the brand of the TV, so you don't need to know the details of how to turn on the TV for every kind of vendor; you just know that all TVs have a press turn on button and it will turn the TV on. (Additionally, probably the ACME TV's turn on implementation is different from SUPER TV, but you don't care!).

Software objects also communicate via interfaces. A Java interface describes a set of methods that can be called on an object, to tell the object to perform some task or return some piece of information.

In its most common form, an interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:
interface Bicycle {

void changeCadence(int newValue);

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);
}
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
class ACMEBicycle implements Bicycle {

// remainder of this class implemented as before

}
Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.

Interfaces have another very important role in the Java language and is that unlike C++ Java does not support multiple inheritance. However, a class can extend one superclass and implement as many interfaces as needed. This is very useful when you want to share behavior of unrelated objects. This allows objects of unrelated classes to be processed polymorphically, objects of classes that implement the same interface can respond to the same method calls.

This latter benefit of using interfaces will be discussed later, so if it sounds confusing don't bother much for the moment, and think about interfaces as a way of contract between the class and the outside world, and that this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile. Remember the example provided, if there was an standard interface between bike manufacturers probably its user would have known how to break because all bikes implementing the interface would have the same methods in this case applyBrakes.

No comments:

Post a Comment