Instead of procedures, OO programming relies on software objects as the units of modularity. An individual
object is an instance of a type, or ???class.??? One creates an instance by using the specifications of the class.
As an analogy, my car is an instance of the class automobile??”it has four wheels, a motor, seats, etc., like all
cars do, but my car is one specific car among the many automobiles in the world.
An instance of a class has its own ???state,??? or values of its characteristics. My car is red; your car is blue;
both are automobiles. My car has 170 hp; yours has 200 hp. My car is not moving at this moment; your car is
traveling at 34 mph. The class automobile specifies that all automobiles have color, horsepower, and speed
(among other things) as attributes or instance variables.
Objects also have behavior. Behavior is determined by procedures of the class, and such procedures are
called methods. An automobile class will have methods such as changeSpeed (to accelerate or decelerate),
park, refuel, and turn. Given an instance of the automobile class, a program could invoke the
changeSpeed method, for example, to cause that particular car to go faster. The changeSpeed method is
called an instance method, because it affects an instance of the class automobile (one particular automobile).
Pages:
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148