Encapsulation | 19
Try testing it again. This second time, everything works fine, except, you don’t get
the dog object expressing “Meow.” You see “Woof, woof.”
You may be thinking that private variables really limit what you can do. Suppose you
want the dog object to howl, growl or whimper? How do you make the changes
dynamically? Preventing an encapsulated object from doing something wrong is one
thing, but how can an object be set up to accept variables?
The many meanings of interface
In this book, you will find the terminterfaceused in different contexts, and each con-
text gives the term a slightly different meaning. (Thanks a lot!) Up to this point,
you’re probably familiar with terms like UI (user interface) or GUI (graphic user
interface). These terms refer to different tools you use to interact with a program. For
example, a button is a common UI in Flash. When you click a button, something
predictable happens. You may not know how it happens (or care), but you know
that if you press the button, the video will play, a different page will appear, or an
animation will start. So if you understand the basic concept of a UI, you should be
able to understand how an interface works with an object.
With a UI, the black bo xis the application you’re using, whether it’s shopping at
eBay or using a word processor. If you follow certain rules and use the different UIs
in the appropriate manner, you get what you want. In the same way, an encapsu-
lated object is a black box, and the interface describes the ways you can interact with
it programmatically. It’s the UI for the object.
Design Patterns: Elements of Reusable Object-Oriented Software(page 13) nicely
clarifies object interfaces and their signatures. An object’ssignatureis its operation
name, parameters, and return datatype. Figure 1-5 graphically shows the makeup of
a typical object’s signature.
All of an object’s signatures defined by its operations is the interface. In this context
then,the interface for the object constitutes the rules for access, list of services, and
controls for it.
Figure 1-5. Object’s signature
public function myMethod (myParam:String):String
{
opVar=myParam;
return opVar;
}
Operation name Parameters Return value
Signature