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.

234<br />

How Object-Oriented Programming Works<br />

There’s no single “right” way to divide a large program into objects. Two programmers<br />

tackling the same problem will likely divide the same program<br />

into different objects. The way you define your objects reflects how you view<br />

a particular problem.<br />

Every object consists of two parts, as shown in Figure 7-3:<br />

✦ Data (also called properties)<br />

✦ Subprograms (also called methods)<br />

Object<br />

Figure 7-3:<br />

The parts of<br />

a typical<br />

object.<br />

Data<br />

Subprograms<br />

Objects contain two types of data:<br />

✦ Public data is accessible by other parts of the program.<br />

✦ Private data within the object is hidden from the rest of the program.<br />

Objects contain two types of subprograms:<br />

✦ Public subprograms allow other parts of a program to control an object.<br />

✦ Private subprograms are used by an object to calculate a result needed<br />

by its public subprograms (methods).<br />

The difference between public and private data and subprograms is<br />

accessibility:<br />

✦ Public data and subprograms are what the rest of a program can “see”<br />

and use in an object:<br />

• Public data typically describes the object in some way. For example,<br />

a video game program might create an object that represents a monster.<br />

This object may need data, representing X and Y coordinates, to<br />

define the monster’s location on-screen.<br />

• Public subprograms (methods) allow other parts of a program to<br />

manipulate an object. For example, an object representing a monster

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

Saved successfully!

Ooh no, something went wrong!