«^»
8.2. The class Circle

Suppose we now want a class Circle to represent shapes that are circles. We can create this class by inheritance from the class Shape as follows:

0607: public class Circle extends Shape {                           // Circle.java
0608:    public Circle(int vRadius, int vX, int vY) {
0609:       super(vX, vY);  iRadius = vRadius;
0610:    }
0611:    public Circle() { this(0, 0, 0); }
0612:    public int getRadius() { return iRadius; }
0613:    public boolean equals(Object rObject) { 
0614:       return super.equals(rObject) && iRadius == ((Circle)rObject).iRadius;
0615:    }
0616:    public String toString() { return super.toString() + ":" + iRadius; }
0617:    private int iRadius;
0618: }

Objects of this class have three fields iX, iY and iRadius. Once again, the constructor for this class uses the special method called super:

0619: super(vX, vY);
in order to initialize the iX and iY fields (with the values that are passed through vX and vY).

The bodies of the equals and the toString methods show a different use of the super keyword. In these methods, it appears as super.methodname(...). This notation means: ‘apply the method methodname as defined in the superclass to the current object’.

In this example, instead of using super.methodname(...), the getX and getY methods of the superclass could be used. For example, toString could be declared as:

0620: public String toString() { return getX() + ":" + getY() + ":" + iRadius; }
Note that:
0621: public String toString() { return iX + ":" + iY + ":" + iRadius; }
would not be possible unless the iX and iY fields of the class Shape were changed from private fields to protected fields.