Information Technology Reference
In-Depth Information
role in the functionality of that object. Many of the actions that are performed by
or to an object are based on the objects current state.
Programs are written so that a change in the state in an object will in some way
affect the program as a whole. For example, the constant monitoring of certain
object states is the driving force behind event-driven programming styles, which
create programs that react to the occurrence of certain events (specified changes in
object states). In this manifestation of object-oriented programming, a program
might contain a user interface (UI) that listens for the user to perform a predefined
action. For instance, the UI might contain a button that the user can click. When
the user clicks said button, the state of some related object changes to reflect that
the button has been clicked. This state change then triggers some associated
reaction in the program. In this way, the operation of, and progression through, the
program is dictated by the states of the objects in it.
We have said that objects are able to both change states and react to state
changes. Of course, however, an object cannot simply 'know' how to change
states, or how to react to another object's state change. Rather, objects are coded
with methods that perform tasks when called upon to do so. Think again of the
monster truck from before. Monster truck contains a method, which is
derived from the truck class (a concept called inheritance, which will be dis-
cussed later on the chapter), that turns the engine on. This method might be called
start , and it reacts to two specific events: the presence of a key in the ignition,
and the turning of that key to the right position. Essentially then, the monster
truck object reacts to certain state changes (placing the key in the ignition and
turning it) by calling the method start , which turns the vehicle on. This method
was implemented in the object by an engineer, much in the way that a software
engineer implements methods in a software object.
The methods that are included in an object can vary greatly, as different objects
are created for different purposes. There are two methods, however, that are
common to most objects: constructors and destructors. Construction is the action
of creating an object from its respective class, and initializing its attributes with
either assigned values or those that are given to it by default. Object construction
physically places that object into the computer's memory. An object's constructor
is the actual method that handles the creation and initialization of the object.
Likewise, destruction is the action of erasing an object, and thus freeing up the
memory in which it resided. The destructor method of an object performs this
action. Destruction of an object is used to provide room in the system memory,
potentially for the creation of other objects. It also prevents faulty code from
creating memory leaks. Constructors and destructors will be discussed in greater
detail in Sect. 2.3.4 .
Fig. 2.1
Real world objects
CAR
TIM
LION
Search WWH ::




Custom Search