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
Establishing Inheritance
You establish a class as the subclass of another class with the extends statement, as in the following:
class AnimatedLogo extends javax.swing.JApplet {
// behavior and attributes go here
}
The extends statement establishes the AnimatedLogo class of objects as a subclass of JApplet, using the full class name of javax.swing.JApplet. As you will see during Hour 17, "Creating Interactive Web Programs," all Swing applets must be subclasses of JApplet because they need the functionality this class provides to run on a World Wide Web page.
One method that AnimatedLogo will have to override is the paint() method, which is used to draw all things that are shown on the program's display area. The paint() method is implemented by the Component class and is passed all the way down to AnimatedLogo. However, the paint() method does not do anything. It exists so that subclasses of Component have a method they can use when something must be displayed when that subclass is running.
To override a method, you must start the method in the same way it started in the superclass from which it was inherited. A public method must remain public, the value sent back by the method must be the same, and the number and type of arguments to the method must not change.
The paint() method of the Component class begins as follows:
public void paint(Graphics g) {
When AnimatedLogo overrides this method, it must begin with a statement like this:
public void paint(Graphics screen) {
The only difference is in the name of the Graphics object, which does not matter when determining if the methods are created in the same way. These two statements match because the following things match:
- Both paint() methods are public.
- Both methods return no value, as declared by the use of the void statement.
- Both have a Graphics object as their only argument.
Using this and super in a Subclass
Two keywords that are extremely useful in a subclass are this and super.
As you learned during the previous hour, the this keyword is used to refer to the current object. When you're creating a class and you need to refer to the specific object created from that class, this can be used, as in the following statement:
this.title = "Cagney";
This statement sets the object's title variable to the text Cagney.
The super keyword serves a similar purpose: It refers to the immediate superclass of the object. super can be used in several different ways:
- To refer to a constructor method of the superclass, as in super("Adam", 12).
- To refer to a variable of the superclass, as in super.hawaii = 50.
- To refer to a method of the superclass, as in super.dragNet().
One of the ways you'll use the super keyword is in the constructor method of a subclass. Because a subclass inherits all the behavior and attributes of its superclass, you have to associate each constructor method of that subclass with a constructor method of its superclass. Otherwise, some of the behavior and attributes might not be set up correctly, and the subclass won't be able to function properly.
To make sure that this happens, the first statement of a subclass constructor method must be a call to a constructor method of the superclass. This requires the super keyword, as in the following statements:
public ReadFiles(String name, int length) {
super(name,length);
}
This example is the constructor method of a subclass, and it uses super(name,length) to call a comparable constructor in its superclass.
Working with Existing Objects | Next Section

Account Sign In
View your cart