Monday, October 18, 2010

Create Sublass In Java


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.
Listing 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 = x;
 8:         super.y = y;
 9:         this.z = z;
10:     }
11:
12:     public void move(int x, int y, int z) {
13:         this.z = z;
14:         super.move(x, y);
15:     }
16:
17:     public void translate(int x, int y, int z) {
18:         this.z += z;
19:         super.translate(x, y);
20:     }
21: }

Compile this file with the javac compiler tool, and 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 work 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 statements super and this. The this statement is used to refer to the current Point3D object, so this.z = z; in Line 9 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. A subclass that overrides a method still can call the original method with the super statement. An example of this is Line 14, which calls the move() method of Point to set 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 that 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.
Listing 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 and y 15:         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 and z 27:         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 the java interpreter, the following should be shown:
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
When people talk about the miracle of birth, they're probably not speaking of the way a superclass can give birth to subclasses or the way behavior and attributes are inherited in a hierarchy of classes. However, if the real world worked the same way that object-oriented programming does, every grandchild of Mozart would get to choose whether to be a brilliant composer. All descendants of Mark Twain could wax poetic about Mississippi riverboat life. Every skill your direct ancestors worked to achieve would be handed to you without an ounce of toil.
On the scale of miracles, inheritance isn't quite up to par compared with continuing the existence of a species and getting a good tax break. However, it's an effective way to design software with a minimum of redundant work.

No comments:

Post a Comment