15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Encapsulation Isolates Data and Subprograms 235<br />

might include a Move subprogram that can change the value of the<br />

object’s X and Y coordinates (to determine where to display the cartoon<br />

monster on-screen).<br />

✦ Private data and subprograms are what an object uses to do something<br />

useful, so the object doesn’t need to allow other parts of the program to<br />

access this in<strong>for</strong>mation.<br />

The Monster object might contain a private subprogram that calculates<br />

exactly how the Monster object moves. Because other parts of the program<br />

don’t need to know exactly how the Monster object calculates its movement,<br />

this type of in<strong>for</strong>mation would be private and hidden from the rest of the<br />

program.<br />

Ultimately, OOP is another way to make <strong>programming</strong> easier. Just as highlevel<br />

languages (like BASIC) simplify <strong>programming</strong> by using real-life commands<br />

(such as PRINT), so does OOP simplify organizing programs by<br />

modeling real-life items. The three advantages that objects have over ordinary<br />

subprograms include<br />

✦ Encapsulation<br />

✦ Inheritance<br />

✦ Polymorphism<br />

Book II<br />

Chapter 7<br />

Breaking a<br />

Large Program<br />

into Objects<br />

Object-oriented <strong>programming</strong> provides tools <strong>for</strong> making <strong>programming</strong> easier,<br />

but it’s still possible to write horrible software with OOP. Think of OOP like<br />

lines painted on the highway. If you follow the lines, you probably arrive<br />

safely at your destination, but if you ignore the lines and do whatever you<br />

want, you probably crash your car. Like traffic lines painted on the road,<br />

OOP guides you into writing software that can be created and modified<br />

easily, but you can still mess up things if you’re not careful.<br />

Encapsulation Isolates Data and Subprograms<br />

Subprograms have two problems. First, subprograms can work with data<br />

from any part of a program. That’s what makes subprograms useful, but<br />

that’s also what makes subprograms harder to modify and fix. If you don’t<br />

know what data a subprogram might manipulate and when, any changes you<br />

make to that subprogram could affect a program in unpredictable ways.<br />

For example, suppose someone writes a weather <strong>for</strong>ecasting program that<br />

has a subprogram <strong>for</strong> predicting tomorrow’s temperature measured in<br />

Fahrenheit. What happens if another programmer modifies this subprogram<br />

to <strong>for</strong>ecast temperatures in Celsius?<br />

Figure 7-4 shows two phases of a program:

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

Saved successfully!

Ooh no, something went wrong!