Interface


Think of an interface as a go-between, much like your television remote control. When you press the On button, the remote control relays your request to the appropriate component on the television. The component handles all the complexities involved in powering up your television.

You also use the same On button on the same remote control to turn on your VCR, DVD player, and other televisions . The remote control is programmed to know how to relay your request to these other devices. You only need to learn how to use buttons on the remote control. You don t need to learn what happens after you press a remote control button.

The remote control is an interface. Buttons on the remote control are methods , and pressing a button is calling a method. Each button is a form of polymorphism and, in a sense, is overloaded. That is, you press the same On button to turn on the television, VCR, and DVD player. The remote control knows which device to turn on (behavior) by the other buttons you press on the remote control (argument list). For example, if you press the VCR button and then the On button, the remote control knows to turn on the VCR.

Interface Design

Some companies have standard interfaces for objects and behaviors that are shared among applications within their firm. A standard interface specifies a method name , argument list, return value, and behavior.

For example, we can say that the Display() method is an interface used to display attributes of objects. Previously in this chapter, you saw two examples of Display() methods: member functions, and static or non-class methods that were overloaded. For the overloaded examples, the name of the method is Display() , and it has one argument ”a reference to an object. The behavior of the Display() method is to display attributes of the object passed to it.

An application programmer who wants to display attributes of an object calls the Display() method for the class, or the static/ non-member Display method, and passes it a reference to an object. The application programmer is not concerned how the Display() method displays those attributes.

The programmer who designs the object used by the application programmer must define a method that adheres to the interface standard. The object and the method are then provided to the application programmer. The application programmer then uses the interface to call the behavior from within the program.

Some programmers refer to polymorphism as one interface that has multiple methods. The interface enables an application programmer to enlist a general behavior while the situation determines the exact behavior that is performed. For example, in the static method examples, Display() is the general behavior, and the object passed to the Display() method is the situation that determines which version of the Display() method is called.

The application programmer must know only the general behavior needed to be performed by the program and then call the appropriate interface. The compiler then takes over and determines the specific behavior to perform.




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net