Objects in Action

Consider the case of a program that displays a pie chart. A PieChart object could consist of the following:

  • Behavior to calculate how big each pie slice should be in the chart
  • Behavior to draw the chart
  • An attribute to store the title of the chart

When you compare a pie chart in the real world to a PieChart object in a computer program, it might seem odd to ask the PieChart object to draw itself. Graphs don't draw themselves in the real world. However, objects in object-oriented coding work for themselves whenever possible. This quality makes them more useful because you can incorporate them in other programs without having as many things to teach them. If a PieChart object did not know how to draw itself, for instance, every time you used that PieChart object somewhere, you would have to create behavior to draw it. For another example of object-oriented programming, consider the autodialer program that Matthew Broderick's character used in War Games to find computers he could break into.

By the way

An autodialer is software that uses a modem to dial a series of phone numbers in sequence. The purpose of such a program is to find other computers that answer their own phone, so you can call them up later to see what they are.

Using an autodialer today practically guarantees you'll be on a first-name basis with your local phone company. In the early '80s, it was a good way to be rebellious without actually leaving the house. David Lightman (the character portrayed by Broderick) used his autodialer to look for a video game company's private computer system so he could play the company's new game before it was released. Instead, Lightman found a secret government computer that could play everything from chess to Global Thermonuclear War. An autodialer, like any computer program, can be thought of as a group of objects that work together. It could be broken down into the following:

  • A Modem object, which knows how to make the modem dial a number and how to report when another computer system has answered a call
  • A Monitor object, which keeps track of what numbers were called and which ones were successful, and can save this information for later inspection

Each object exists independently of the other. The Modem object does its job without requiring any help from the Monitor object. One of the advantages of having a completely independent Modem object is that it could be used in other programs that need modem functionality. If David Lightman returns to hacking in War Games 2006 after a bitter divorce from Jennifer Mack (Ally Sheedy's character), he could use the Modem object as part of an elaborate ATM fraud scheme. Another reason to use self-contained programs such as objects is that they are easier to debug. Computer programs quickly become unwieldy in size. If your program is just one big list of instructions, you can't change one part without making sure it won't damage the performance of other parts that are dependent on it. If you're debugging something like a Modem object, though, you know it's not dependent on anything else. You can focus on making sure the Modem object does the job it's supposed to do and holds the information that it needs to do its job. For these reasons, object-oriented coding is becoming the norm in many areas of software development. Learning an object-oriented language like Java as your first coding language can be an advantage in some ways because you're not unlearning the habits of other styles of programming. The main disadvantage is that an object-oriented language can be more challenging to learn than a non-object-oriented language such as Visual Basic.