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.

Organizing a Program with Object-Oriented Programming 47<br />

in the original subprogram and also in the modified subprogram. If<br />

you made 20 different modifications to a subprogram, you now have<br />

the problem of not only correcting the error in every copy of the<br />

original subprogram, but also fixing that same error in all your<br />

modified versions of that original subprogram.<br />

But after you modify a subprogram, will you remember which subprogram<br />

you copied and modified originally? Even worse, you could copy a subprogram<br />

and modify it, and then copy your modified subprogram and modify<br />

that copy. Do this several times and you’ll wind up with several slightly<br />

different versions of the same subprogram, but now you may not have any<br />

idea which subprogram you copied originally.<br />

Book I<br />

Chapter 2<br />

Different Methods<br />

<strong>for</strong> Writing<br />

Programs<br />

So now if you find an error in the original subprogram, how can you find and<br />

fix that same error in any modified copies of that subprogram? Most likely,<br />

you can’t because you won’t know <strong>for</strong> sure which modified versions of the<br />

subprogram you (or another programmer) might have created.<br />

Because programmers are always going to copy an existing program that<br />

works, object-oriented <strong>programming</strong> helps manage the copying process by<br />

using inheritance. The whole idea behind inheritance is that rather than<br />

making physical copies of a subprogram, you have only one copy of a subprogram<br />

at all times.<br />

Instead of physically copying a subprogram, objects inherit a subprogram by<br />

essentially pointing to the subprogram that they want to copy. Not only does<br />

this save physical space by eliminating the need to make copies of a subprogram,<br />

but this also makes it easy to modify subprograms.<br />

If you find an error in a subprogram, just correct the error in the original<br />

subprogram and that’s it. Any objects that have inherited commands from<br />

that subprogram now point automatically to the modified version of the original<br />

subprogram, as shown in Figure 2-8.<br />

By isolating commands in objects and using inheritance, objects can get the<br />

advantages of copying subprograms without the disadvantages of having<br />

multiple physical copies scattered all over the place.<br />

Object-oriented <strong>programming</strong> makes programs easier to write (by dividing a<br />

large program into parts), easier to understand (by organizing subprograms<br />

into objects that mimic the actual problem the program is trying to solve),<br />

and easier to modify (by automatically updating any copies of subprograms).<br />

All these advantages allow you, as the programmer, to focus more on solving<br />

problems and less on keeping track of trivial details.

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

Saved successfully!

Ooh no, something went wrong!