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
- The Power of Inheritance
- Establishing Inheritance
- Working with Existing Objects
- Workshop: Creating a Subclass
- Summary
- Q&A
- Quiz
- Activities
- 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
Workshop: Creating a Subclass
To see an example of inheritance at work, you will create a class called Point3D that represents a point in three-dimensional space. A two-dimensional point can be expressed with an (x,y) coordinate. Applets use an (x,y) coordinate system to determine where text and graphics should be displayed. Three-dimensional space adds a third coordinate, which can be called z.
The Point3D class of objects should do three things:
- Keep track of an object's (x,y,z) coordinate.
- Move an object to a new (x,y,z) coordinate when needed.
- Move an object by a certain amount of x, y, and z values as needed.
Java already has a standard class that represents two-dimensional points; it's called Point. It has two integer variables called x and y that store a Point object's (x,y) location. It also has a move() method to place a point at the specified location, and a translate() method to move an object by an amount of x and y values.
Run your word processor and create a new file called Point3D.java. Enter the text of Listing 12.1 into the file, and save it when you're done.
Example 12.1. The Full Text of Point3D.java
1: import java.awt.*;
2:
3: public class Point3D extends Point {
4: public int z;
5:
6: public Point3D(int x, int y, int z) {
7: super(x,y);
8: this.z = z;
9: }
10:
11: public void move(int x, int y, int z) {
12: this.z = z;
13: super.move(x, y);
14: }
15:
16: public void translate(int x, int y, int z) {
17: this.z += z;
18: super.translate(x, y);
19: }
20: }
After you compile this file with a Java compiler such as javac, you will have a class you can use in programs. The Point3D class does not have a main() block statement, so you cannot run it with the java interpreter.
The Point3D class only has to do work that isn't being done by its superclass, Point. This primarily involves keeping track of the integer variable z, and receiving it as an argument to the move() method, translate() method, and Point3D() constructor method.
All of the methods use the keywords super and this. The this statement is used to refer to the current Point3D object, so this.z = z; in Line 8 sets the object variable z equal to the z value that was sent as an argument to the method in Line 6.
The super statement refers to the superclass of the current object, Point. It is used to set variables and call methods that were inherited by Point3D. The statement super(x,y) in Line 7 calls the Point(x,y) constructor in the superclass, which then sets the (x,y) coordinates of the Point3D object. Because Point already is equipped to handle the x and y axes, it would be redundant for the Point3D class of objects to do the same thing.
To test out the Point3D class you have compiled, create a program that uses Point and Point3D objects and moves them around. Create a new file in your word processor and enter Listing 12.2 into it. Save the file as TryPoints.java.
Example 12.2. The Full Text of TryPoints.java
1: import java.awt.*;
2:
3: class TryPoints {
4: public static void main(String[] arguments) {
5: Point object1 = new Point(11,22);
6: Point3D object2 = new Point3D(7,6,64);
7:
8: System.out.println("The 2D point is located at (" + object1.x
9: + ", " + object1.y + ")");
10: System.out.println("\tIt's being moved to (4, 13)");
11: object1.move(4,13);
12: System.out.println("The 2D point is now at (" + object1.x
13: + ", " + object1.y + ")");
14: System.out.println("\tIt's being moved -10 units on both the x "
15: + "and y axes");
16: object1.translate(-10,-10);
17: System.out.println("The 2D point ends up at (" + object1.x
18: + ", " + object1.y + ")\n");
19:
20: System.out.println("The 3D point is located at (" + object2.x
21: + ", " + object2.y + ", " + object2.z +")");
22: System.out.println("\tIt's being moved to (10, 22, 71)");
23: object2.move(10,22,71);
24: System.out.println("The 3D point is now at (" + object2.x
25: + ", " + object2.y + ", " + object2.z +")");
26: System.out.println("\tIt's being moved -20 units on the x, y "
27: + "and z axes");
28: object2.translate(-20,-20,-20);
29: System.out.println("The 3D point ends up at (" + object2.x
30: + ", " + object2.y + ", " + object2.z +")");
31: }
32: }
After you compile this file and run it with a Java interpreter, the following output should be displayed:
The 2D point is located at (11, 22)
It's being moved to (4, 13)
The 2D point is now at (4, 13)
It's being moved -10 units on both the x and y axes
The 2D point ends up at (-6, 3)
The 3D point is located at (7, 6, 64)
It's being moved to (10, 22, 71)
The 3D point is now at (10, 22, 71)
It's being moved -20 units on the x, y and z axes
The 3D point ends up at (-10, 2, 51)
Summary | Next Section

Account Sign In
View your cart