15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

236<br />

Encapsulation Isolates Data and Subprograms<br />

The upper phase shows the main program sending the current temperature<br />

(32 degrees Fahrenheit) to the <strong>for</strong>ecasting subprogram, which then<br />

returns its prediction (as 30 degrees Fahrenheit).<br />

The lower phase shows the same program except now the <strong>for</strong>ecasting<br />

subprogram has been modified to return Celsius temperatures. So now<br />

when the main program sends the current temperature (in Fahrenheit)<br />

to the <strong>for</strong>ecasting subprogram, this subprogram returns its <strong>for</strong>ecast in<br />

Celsius. The main program now uses this faulty value.<br />

Figure 7-4:<br />

Changing a<br />

subprogram<br />

can wreck a<br />

perfectly<br />

working<br />

program.<br />

Main program<br />

Forecast temperature (32)<br />

Display temperature<br />

Tomorrow’s <strong>for</strong>ecast =<br />

30 Degrees<br />

This program measures<br />

temperatures in Fahrenheit.<br />

Subprogram Forecast<br />

Return tomorrow’s temperature<br />

This subprogram also measures<br />

temperatures in Fahrenheit.<br />

Main program<br />

Forecast temperature (32)<br />

Display temperature<br />

Tomorrow’s <strong>for</strong>ecast =<br />

–1.11 Degrees<br />

This program expects to receive<br />

a temperature in Fahrenheit, so<br />

it now displays the wrong <strong>for</strong>ecast.<br />

Subprogram Forecast<br />

Return tomorrow’s temperature<br />

This subprogram now converts<br />

Fahrenheit temperatures into<br />

Celsius and returns a<br />

temperature measured in Celsius.<br />

This problem occurs because the <strong>for</strong>ecasting subprogram has no idea how<br />

its data is being used by another part of the program. Object-oriented <strong>programming</strong><br />

can partially solve this problem by organizing data, and all the<br />

subprograms that manipulate that data, into a single location, or an object.<br />

By grouping data and all the subprograms that manipulate that data in one<br />

place, it’s much easier to understand how that data is being used.<br />

The whole idea behind an object is to isolate and “hide” data and subprograms<br />

by using encapsulation. Encapsulation acts like a wall, as shown in<br />

Figure 7-5, that wraps around data and subprograms to<br />

✦ Keep other parts of a program from manipulating data inside an<br />

object.<br />

✦ Keep subprograms inside that object from manipulating data outside<br />

that object.<br />

✦ Keep programmers from modifying code stored in another object.<br />

Shielding data inside an object<br />

Think of data as a wallet full of cash. The more people who handle your<br />

wallet be<strong>for</strong>e giving it back to you, the greater the chance that someone<br />

takes money out of that wallet (manipulating the data). Ideally, you want as<br />

few people to handle your wallet as possible and if people absolutely must<br />

handle your wallet, you want them close enough so you can keep an eye on<br />

them.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!