236 Encapsulation Isolates Data and Subprograms The upper phase shows the main program sending the current temperature (32 degrees Fahrenheit) to the forecasting subprogram, which then returns its prediction (as 30 degrees Fahrenheit). The lower phase shows the same program except now the forecasting subprogram has been modified to return Celsius temperatures. So now when the main program sends the current temperature (in Fahrenheit) to the forecasting subprogram, this subprogram returns its forecast in Celsius. The main program now uses this faulty value. Figure 7-4: Changing a subprogram can wreck a perfectly working program. Main program Forecast temperature (32) Display temperature Tomorrow’s forecast = 30 Degrees This program measures temperatures in Fahrenheit. Subprogram Forecast Return tomorrow’s temperature This subprogram also measures temperatures in Fahrenheit. Main program Forecast temperature (32) Display temperature Tomorrow’s forecast = –1.11 Degrees This program expects to receive a temperature in Fahrenheit, so it now displays the wrong forecast. Subprogram Forecast Return tomorrow’s temperature This subprogram now converts Fahrenheit temperatures into Celsius and returns a temperature measured in Celsius. This problem occurs because the forecasting subprogram has no idea how its data is being used by another part of the program. Object-oriented programming can partially solve this problem by organizing data, and all the subprograms that manipulate that data, into a single location, or an object. By grouping data and all the subprograms that manipulate that data in one place, it’s much easier to understand how that data is being used. The whole idea behind an object is to isolate and “hide” data and subprograms by using encapsulation. Encapsulation acts like a wall, as shown in Figure 7-5, that wraps around data and subprograms to ✦ Keep other parts of a program from manipulating data inside an object. ✦ Keep subprograms inside that object from manipulating data outside that object. ✦ Keep programmers from modifying code stored in another object. Shielding data inside an object Think of data as a wallet full of cash. The more people who handle your wallet before giving it back to you, the greater the chance that someone takes money out of that wallet (manipulating the data). Ideally, you want as few people to handle your wallet as possible and if people absolutely must handle your wallet, you want them close enough so you can keep an eye on them.
Encapsulation Isolates Data and Subprograms 237 Object Data Subprograms Figure 7-5: Encapsulation isolates a chunk of code as an independent object. Other programmers Data Subprograms Other objects That’s the same idea behind encapsulating data inside an object. In a program divided into multiple subprograms, data gets passed around like a hot potato. The more subprograms capable of changing a chunk of data, the more likely one of those subprograms can accidentally change that data incorrectly. Book II Chapter 7 Breaking a Large Program into Objects By encapsulating data inside of an object, you prevent anything outside that object from manipulating the data. Grouping subprograms inside of an object After you isolate data inside an object, you also need to isolate all the subprograms that manipulate that data inside that same object. By storing all subprograms that manipulate the same data, objects make it easy to isolate any problems. If data inside an object gets messed up, the faulty subprogram can be located only inside that same object. This makes troubleshooting easier. In comparison, if data gets messed up in a non-object-oriented program, the faulty subprogram could be located anywhere. Trying to find a faulty subprogram in an object is like trying to find your lost keys in your apartment. Trying to find a faulty subprogram in an entire program is like trying to find your lost keys in a 20-story apartment building.