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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.