46 Organizing a Program with Object-Oriented Programming Suppose you wrote a program to calculate a rocket’s trajectory to the moon, and the engineers suddenly designed the rocket with a more powerful engine? With object-oriented programming, you could just yank the engine object out of your program, rewrite or replace it, and plug it back into the program again. In structured programming, modifying the program to reflect a new rocket engine would mean finding the program commands that manipulate the data that represents the engine’s thrust, and then making sure that new data gets fed into the program at the proper location and still works with any other program commands that also handle that same data. (If the explanation in this paragraph sounded confusing and convoluted to you, that just shows you the less-than-intuitive problem of modifying a structured program versus an object-oriented program.) Objects simplify modifications Besides organizing a large program into logical pieces, objects have another purpose — code reusability. Just as in high school, it was always easier to copy someone else’s homework rather than do it yourself, so programmers find that it’s easier to copy and reuse somebody else’s program rather than write their own 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’ll have to fix that subprogram in every copy. If you made 17 copies of a subprogram, you’ll have to fix the same error 17 times in 17 different copies of the same subprogram. ✦ What if you want 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 either • Write 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 this copy. Now you’ll 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
Organizing a Program with Object-Oriented Programming 47 in the original subprogram and also in the modified subprogram. If you made 20 different modifications to a subprogram, you now 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. Book I Chapter 2 Different Methods for Writing Programs 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 rather than making physical copies of a subprogram, you have only one copy of a subprogram at all times. Instead of physically copying a subprogram, objects inherit a subprogram by essentially pointing to the subprogram that they want to copy. Not only does this save physical space by eliminating the need to make copies of a subprogram, but this also makes it easy to modify subprograms. If you find an error in a subprogram, just correct the error in the original subprogram and that’s it. Any objects that have inherited commands from that subprogram now point automatically to the modified version of the original subprogram, as shown in Figure 2-8. By isolating commands in objects and using inheritance, objects can get the advantages of copying subprograms without the disadvantages of having multiple physical copies scattered all over the place. Object-oriented programming makes programs easier to write (by dividing a large program into parts), easier to understand (by organizing subprograms into objects that mimic the actual problem the program is trying to solve), and easier to modify (by automatically updating any copies of subprograms). All these advantages allow you, as the programmer, to focus more on solving problems and less on keeping track of trivial details.
Agreed in 2016, the motive of the General Data Protection Regulation (GDPR) is to better protect the personal data of European Union “data subjects” – EU citizens and other nationals physically present in the EU at the time data are collected. Visit: https://www.hipaajournal.com/gdpr-training/