Page 231 - Introduction to Programming with Java: A Problem Solving Approach
P. 231

                6.2 Object-Oriented Programming Overview 197
An object’s state refers to the characteristics that currently define the object. For example, if you’re writing a program that keeps track of employee salaries, you’d probably want to have employee objects, where an employee object’s state consists of the employee’s name and current salary.
An object’s behaviors refer to the activities associated with the object. Once again, if you’re writing a program that keeps track of employee salaries, you’d probably want to define a behavior that adjusts an em- ployee’s salary. That type of behavior parallels a real-world behavior—a pay raise or a pay cut. In Java, you implement an object’s behaviors as methods. For example, you’d implement the salary adjustment behavior as an adjustSalary method. We’ll describe method implementation details shortly. But it’s important to complete our OOP overview first.
Here are some entities that would make good candidates for objects in an object-oriented program:
Physical Objects
cars in a traffic-flow simulation
aircraft in an air-traffic control system
electrical components in a circuit-design program
Human Objects
employees customers students
Mathematical Objects
points in a coordinate system complex numbers
time
   Let’s think about the first example object. If a car is considered to be an object in a traffic-flow-simula- tion program, what is the data stored in each car object? In order to analyze traffic flow, each car’s position and speed should be monitored. Therefore, those two pieces of data should be stored as part of a car object’s state. And what behaviors are associated with the car objects? You’d need to be able to start the car, stop the car, slow down, and so on. So you’d probably want to implement these methods:
start, stop, slowDown
An object’s behaviors can change an object’s state. For example, a car object’s start method causes the Apago PDF Enhancer
car’s position and speed data items to change.
Encapsulation
Objects provide encapsulation. In general terms, encapsulation is when something is wrapped up inside a protective covering. When applied to objects, encapsulation means that an object’s data are protected by be- ing “hidden” inside the object. With hidden data, how can the rest of the program access an object’s data? (Accessing an object’s data refers to either reading the data or modifying it.) The rest of the program cannot access an object’s data directly, but it can access the data with the help of the object’s methods. Assuming an object’s methods are well written, the methods ensure that data is accessed in an appropriate manner. Returning to the employee-salaries program example, an employee object’s salary should be modified only by calling the adjustSalary method. The adjustSalary method ensures that an employee object’s salary is modified appropriately. For example, the adjustSalary method prevents an employee object’s salary from becoming negative.
See Figure 6.1. It illustrates how an object’s methods form the interface between an object’s data and the rest of the program.
Benefits of OOP
Now that you have a basic idea of what OOP is, you may be asking yourself what all the hype is about. Why is OOP preferred over procedural programming for most of today’s new programs? Here are some benefits of OOP:
• OOP programs have a more natural organization—Since people tend to think about real-world prob- lems in terms of real-world objects, it’s easier for people to understand a program that’s organized around objects.












































































   229   230   231   232   233