Читать книгу Beginning Programming All-in-One For Dummies - Wallace Wang - Страница 45

Simplifying modifications

Оглавление

Besides organizing a large program into logical pieces, objects have another purpose: code reusability. In school, it was always easier to copy someone else’s homework than it was to do it yourself. Similarly, programmers find that it’s easier to copy and reuse somebody else’s program rather than write their own program from scratch.

In structured programming, you could divide a large program into subprograms and then store those subprograms in a separate file. Now you could copy that file to reuse those subprograms in another program.

Copying subprograms makes programming easier, but here are two problems:

 What if you copy a subprogram and then later find an error in that subprogram? Now you have to fix that subprogram in every copy. If you made 17 copies of a subprogram, you’d have to fix the same error 17 times in 17 different copies of the same subprogram.

 What if you wanted to modify and improve a subprogram? Suppose you find a subprogram that asks the user to type in a password of no more than 10 characters, but you want your program to allow users to type in passwords up to 25 characters. At this point, you could eitherWrite your own password-verifying subprogram from scratch (which would take time).Copy the existing subprogram and modify it (which would take much less time). It’s easier to make a copy of an existing subprogram and then modify the copy. Now you have two copies of (almost) the same subprogram, but uh-oh, suddenly you discover an error in the original subprogram. Once again, you have to correct this error in the original subprogram and also in the modified subprogram. If you made 20 different modifications to a subprogram, you would have the problem of not only correcting the error in every copy of the original subprogram, but also fixing that same error in all your modified versions of that original subprogram.

But after you modify a subprogram, will you remember which subprogram you copied and modified originally? Even worse, you could copy a subprogram and modify it, and then copy your modified subprogram and modify that copy. Do this several times and you’ll wind up with several slightly different versions of the same subprogram, but now you may not have any idea which subprogram you copied originally.

So, now if you find an error in the original subprogram, how can you find and fix that same error in any modified copies of that subprogram? Most likely, you can’t because you won’t know for sure which modified versions of the subprogram you (or another programmer) might have created.

Because programmers are always going to copy an existing program that works, object-oriented programming helps manage the copying process by using inheritance. The whole idea behind inheritance is that instead of making physical copies of code, you have only one copy of code (called a class) at all times.

Instead of physically copying all the code stored in a class, objects inherit all the code in a class by essentially pointing to the subprogram that they want to copy. This saves physical space by eliminating the need to make multiple copies of the same code.

Now instead of copying code (and risking creating duplicate copies), you can reuse existing code and add your own code. Now you can build more complicated programs by reusing existing objects like building blocks. If you modify code in one class, that modification automatically appears in any classes that inherit from that original class, as shown in Figure 2-8.


FIGURE 2-8: Object-oriented programming never physically copies code but “points to” or “inherits” code.

Object-oriented programming makes programs easier to write (by dividing a large program into parts), easier to understand (by organizing code into classes that mimic the actual problem the program is trying to solve), and easier to modify (by automatically updating any copies of code). All these advantages allow you, as the programmer, to focus more on solving problems and less on keeping track of trivial details.

Discover more about the details of object-oriented programming in Book 2, Chapter 7. For now, it’s just important that you understand why programmers use object-oriented programming. Then you can worry about figuring out how to use object-oriented programming.

Beginning Programming All-in-One For Dummies

Подняться наверх