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
How Object-Oriented Programming Works
The programs you create with Java can be thought of as objects, just like physical objects that exist in the real world. Objects exist independently of other objects, interact in specific ways, and can often be combined with other objects to form something. Examples of objects in the real world include ham sandwiches, the Nixon tapes, a stereo receiver, and my father-in-law Clint. Examples of objects in a computer include a Web browser, the Java compiler, an MP3 file, and a Macromedia Flash plug-in.
If you think of a computer program as a group of objects that interact with each other, you can often come up with a better design for the program that's more reliable, easier to understand, and reusable in other projects.
Later in this book you will create a Java program that displays pie graphs—circles with different-colored pie slices to represent data (Figure 10.1). A pie chart is an object that is made up of smaller objects—individual slices of different colors, a legend identifying what each slice represents, and a title.
Figure 10.1 A Java program that displays a pie chart.
Each object has things that make it different from other objects. Pie charts are circular, while bar graphs represent data as a series of rectangles instead of pie slices. If you break down computer programs in the same way you have broken down a pie chart, you are engaging in object-oriented programming. It's a much less complicated concept than it originally sounds.
In object-oriented programming, an object contains two things: attributes and behavior. Attributes are things that describe the object and show how it is different from other objects. Behavior is what an object does.
You create objects in Java by using a class as a template. A class is a master copy of the object that is consulted to determine which attributes and behavior an object should have. The term class should be familiar to you, because every Java program you have written thus far has been called a class. Every program you create with Java will be a class because each one can be used as a template for the creation of new objects. As an example, any Java program that uses strings is using objects created from the String class. This class contains attributes that determine what a String object is and behavior that controls what String objects can do.
With object-oriented programming, a computer program is a group of objects that work together to get something done. Some simple programs might seem as though they consist of only one object—the class file. However, even those programs are using other objects to get work done.
Objects in Action | Next Section

Account Sign In
View your cart