Saturday, May 7, 2011

OBJECT-ORIENTED PROGRAMMING

Java is an object-oriented language, which means that Java facilitates the writing of object-oriented
programs. Object orientation is more a matter of how one thinks about programming than it is a particular
programming technique. When one writes object-oriented code, one thinks of software “things” that are analogous
to things in the outside world.
For instance, if our job is to write software for the State Thruway Authority for automated toll collection,
we would think about the job differently depending upon whether we take the older “procedural” approach or
the newer object-oriented (OO) approach.

Using the procedural approach, we would break the job down into subtasks, or subroutines, write the
routines, and combine the routines with a main program into a sequence of activities to be performed. We might
have a carEntersThruway procedure, and a carExitsThruway procedure, and we might keep information
in a file about the cars using the Thruway.
Using the OO approach, we would think about the “things” in our problem domain, and create analogous
software classes. For instance, we would probably have a Vehicle class, and we might have a
ThruwayInterchange class, and a VehicleOwner class. We would give Vehicles characteristics that
are important to our application, such as licenseNumber, state, make, model, and color. Since we
need to track where vehicles enter and leave the Thruway, we might add Vehicle properties
enteredAtInterchange and exitedAtInterchange. Likewise, the properties of a VehicleOwner
might include firstName, lastName, streetAddress, city, state, and zipCode.
The logic of an OO program gets coded as “methods” of the objects. Instead of having an isolated
or application-wide carEntersThruway procedure, as we might using the procedural approach, we can
have similar code in a method of the Vehicle class. The logic may be the same, but in the OO approach we
think of a Vehicle object having “behaviors” appropriate to the application. When a Vehicle enters
the Thruway, we will call the Vehicle’s enterThruway method, and the Vehicle object will deal
appropriately with that event.
The data one operates on and the logic one encodes may be the same for the procedural approach and
the object-oriented approach, but the object-oriented approach organizes the work differently. When an objectoriented
program is complete, a set of classes is the result. These classes, if well designed, can be reused
and extended more easily so that future programming projects have a head start.
Putting the code inside classes also allows functionality to be encapsulated, which leads to more effective
testing and more reliable use later on. The software classes exist as their own entities, with known properties
(attributes), and well-defined methods. Using inheritance, the OO approach also offers a standard way to add
functionality without changing at all the code that has already been written. There is less temptation to modify
the classes that have already been defined, implemented and tested. The result is usually more stable and
reliable code.
Object orientation is an advance in how programmers think about their work. The OO approach leads to
software classes which more closely model the real world, and therefore make the application more natural
to think about. In addition, building classes usually leads to code which is more easily reused. Both effects lead
to better programming productivity.

No comments:

Post a Comment