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 programming 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 ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.