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
The Power of Inheritance
Without knowing it, you have used inheritance every time you used one of the standard Java classes such as String or Math. Java classes are organized into a pyramid-shaped hierarchy of classes in which all classes descend from the Object class.
A class of objects inherits from all superclasses that are above it. To get a working idea of how this operates, you'll look at the JApplet class. This class is a superclass of all Swing applets, Java programs written to run on the World Wide Web. The JApplet class, which is used to create Java 2 applets, is a subclass of Applet.
A partial family tree of JApplet is shown in Figure 12.1. Each of the boxes is a class, and the lines connect a superclass to any subclasses below it.
Figure 12.1 The family tree of the Applet class.
At the top is the Object class. JApplet has five superclasses above it in the hierarchy: Applet, Panel, Container, Component, and Object. The JApplet class inherits attributes and behavior from each of these classes because each is directly above it in the hierarchy of superclasses. JApplet does not inherit anything from the five shaded classes in Figure 12.1, which include Dialog and Frame, because they are not above it in the hierarchy.
If this seems confusing, think of the hierarchy as a family tree. JApplet will inherit from its parent, the parent's parent, and on upward. It even might inherit some things from its great-great-grandparent, Object. The JApplet class won't inherit from its siblings or its cousins, however.
Setting up a complicated hierarchy of classes is a difficult thing, but it makes it easier to create new programs later. The amount of work you need to do to write a new class of objects is reduced. Creating a new class boils down to the following task: You only have to define the ways in which it is different from an existing class. The rest of the work is done for you.
As an example, consider the popular video game Tetris. Since the game was invented by Soviet mathematician Alexey Pajitnov, it has been adapted for dozens of different operating systems, processors, and programming languages—including several different Java adaptations. In case you somehow avoided Tetris during the past decade by lapsing into a coma or falling into a deep meditative trance, the game works as follows: Blocks of different shapes fall from the top of the screen, and you must organize them into unbroken horizontal lines before they stack up too high.
The Java source file for several adaptations of Tetris is available for your use. If you wanted to create a new version of Tetris based on one of these existing classes, you could make your game a subclass of an existing Tetris game. All you would have to do is create the things that are new or different about your version, and you'd end up with a new game.
Inheriting Behavior and Attributes
The behavior and attributes of a class are a combination of two things: its own behavior and attributes, and all the behavior and attributes it inherits from its superclasses.
The following are some of the behavior and attributes of Applet:
- The equals() method determines whether an JApplet object has the same value as another object.
- The setBackground() method sets the background color displayed on the applet window.
- The add() method adds user interface components such as buttons and text fields to the applet.
- The setLayout()method defines how the applet's graphical user interface will be organized.
The JApplet class can use all of these methods, even though setLayout() is the only one it didn't inherit from another class. The equals() method is defined in Object, setBackground() comes from Component, and add() comes from Container.
Overriding Methods
Some of the methods defined in the JApplet class of objects were also defined in one of its superclasses. As an example, the update() method is set up in both the JApplet class and the Component class. This method was originally used in Java 1.0 to control whether the applet window is cleared out before anything is drawn in it. In Java 2, the JApplet class has its own update() method that never clears the applet window. When a method is defined in a subclass and its superclass, the subclass method is used. This enables a subclass to change, replace, or completely wipe out some of the behavior or attributes of its superclasses. In the case of update(), the purpose was to wipe out some behavior present in a superclass.
Creating a new method in a subclass to change behavior inherited from a superclass is called overriding the method. You need to override a method any time the inherited behavior will produce an undesired result.
Establishing Inheritance | Next Section

Account Sign In
View your cart