Sams Teach Yourself Java 2 in 24 Hours
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Hour 1. Becoming a Programmer
- Hour 2. Writing Your First Program
- Hour 3. Vacationing in Java
- Hour 4. Understanding How Java Programs Work
- Part II: Learning the Basics of Programming
- Hour 5. Storing and Changing Information in a Program
- Hour 6. Using Strings to Communicate
- Hour 7. Using Conditional Tests to Make Decisions
- Hour 8. Repeating an Action with Loops
- Part III: Working with Information in New Ways
- Hour 9. Storing Information with Arrays
- Hour 10. Creating Your First Object
- Hour 11. Describing What Your Object Is Like
- Hour 12. Making the Most of Existing Objects
- Part IV: Programming a Graphical User Interface
- Hour 13. Building a Simple User Interface
- Hour 14. Laying Out a User Interface
- Hour 15. Responding to User Input
- Hour 16. Building a Complex User Interface
- Part V: Creating Multimedia Programs
- Hour 17. Creating Interactive Web Programs
- Hour 18. Handling Errors in a Program
- Hour 19. Creating a Threaded Program
- Hour 20. Reading and Writing Files
- Part VI: Creating Multimedia Programs
- Hour 21. Using Fonts and Color
- Hour 22. Playing Sound Files
- Hour 23. Working with Graphics
- Hour 24. Creating Animation
- Part VII: Appendixes
- Appendix A. Tackling New Features of Java 2 Version 1.4
- Appendix B. Using the Java 2 Software Development Kit
- Appendix C. Programming with the Java 2 Software Development Kit
- Appendix D. Using Sun ONE Studio
- Appendix E. Where to Go from Here: Java Resources
- Appendix F. This Book's Web Site
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 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 WarGames to find computers he could break into. If you're unfamiliar with the term, 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 WarGames 2003 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 programming is becoming the norm in many areas of software development. Learning an object-oriented language like Java as your first programming 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.
What Objects Are | Next Section

Account Sign In
View your cart