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.2 into the file, and save it when you're done.

Listing 12.2. 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.3 into it. Save the file as TRyPoints.java.

Listing 12.3. 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)


      
Comments