Saturday, May 7, 2011

OBJECT STATE AND BEHAVIOR

Grady Booch, a well-known writer on the topic of OO programming, has defined an object as
something “that has state, behavior, and identity.” In other words, an object has characteristics (which may
change over time), an object can perform certain prespecified actions, and an object provides us a way to
refer to it. The state of an object is provided by variables. As the values of variables change, the state of the object
changes. At any point in time, the values of the instance’s variables provide its state. For example, the speed
of my Ford varies over time. At any particular moment, the state of my Ford includes the speed at which it is
currently moving.
The behavior of objects is provided by methods. A method is a programming procedure. In the case of my
Ford, the accelerate() method allows my Ford to change speed. In the world around us, we observe that
different members of the same class behave similarly; it’s easy to distinguish a dog from a cat by the way the
individual animals move. Likewise, we expect different instances of the same Java class to behave similarly, so
methods are defined by procedures in the class, not in the individual instances. The class Automobile will
have the definition of the accelerate() method, and any particular instance of Automobile will behave
similarly by changing speed using the same method.

No comments:

Post a Comment