Designing Java Classes (part 3)

Now we create a Circle class that is a subclass of Point.

// Circle.java
// Definition of class Circle
import java.awt.Graphics; 

public class Circle extends Point {  // inherits from Point
   protected double radius;

   // No-argument constructor
   public Circle()
   {
      // implicit call to superclass constructor occurs here
	  System.out.println("Default Circle constructor called");
   }

   // Constructor
   public Circle( double a, double b, double r )
   {
      super( a, b );  // call to superclass constructor
      setRadius( r );
	  System.out.println("Circle constructor:" + this);
   }

   // Set radius of Circle
   public void setRadius( double r ) 
      { radius = ( r >= 0.0 ? r : 0.0 ); }

   // Get radius of Circle
   public double getRadius() { return radius; }

   // Calculate area of Circle
   public double area() { return Math.PI * radius * radius; }

   public void draw(Graphics g)
   {
	   int nx = (int) (x*SCALE);
	   int ny = (int) (y*SCALE);
	   int r = (int) (radius*SCALE);
	   int w = 2*r+1;
	   g.drawOval(nx-r,ny-r,w,w);
   }   
   
   // convert the Circle to a String
   public String toString()
   {
      return " Center =" + super.toString() + "; Radius = " + radius;
   }
}

Test Applet

// Test.java
import java.awt.*;
import javax.swing.*;

public class Test extends JApplet {
	Circle circ;
	Point pt;

	public void init()
	{
		pt = new Point(0.1, 0.9);
		circ = new Circle(0.5,0.5,0.3);
	}
	public void destroy()
	{
		pt = null;
		circ = null;
		System.gc();
	}
	public void paint(Graphics g)
	{
		pt.draw(g);
		circ.draw(g);
	}
}

While applet is running:

  

After applet is terminated:

InheritanceTest

// InheritanceTest.java
// Demonstrating the "is a" relationship
import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class InheritanceTest {
   public static void main( String args[] )
   {
      Point pointRef, p; 
      Circle circleRef, c;
      String output;

      p = new Point( .30, .50 );
      c = new Circle( 1.20, .89, 0.15 );

      output = "Point p: " + p.toString() +
               "\nCircle c: " + c.toString();
   
      // use the "is a" relationship to refer to a Circle
      // with a Point reference
      pointRef = c;   // assign Circle to pointRef

      output += "\n\nCircle c (via pointRef): " +
                pointRef.toString();

      // Use downcasting (casting a superclass reference to a
      // subclass data type) to assign pointRef to circleRef
      circleRef = (Circle) pointRef;

      output += "\n\nCircle c (via circleRef): " +
                circleRef.toString();

      DecimalFormat precision4 = new DecimalFormat( "0.0000" );
      output += "\nArea of c (via circleRef): " +
                precision4.format( circleRef.area() );

      // Attempt to refer to Point object
      // with Circle reference
      if ( p instanceof Circle ) {
         circleRef = (Circle) p;  
         output += "\n\ncast successful";
      }
      else
         output += "\n\np does not refer to a Circle";

      JOptionPane.showMessageDialog( null, output,
         "Demonstrating the \"is a\" relationship",
         JOptionPane.INFORMATION_MESSAGE );

      System.exit( 0 );
   }
}

Point class

// Point.java
// Definition of class Point (simple)
import java.awt.Graphics; 

/**
*  Point describes a two-dimensional point.
*/

public class Point
{
	static final double SCALE = 100;
	public double x, y; // coordinates of the Point

	// No-argument constructor
	public Point()
	{
		System.out.println("Point (no-argument constructor called");
	}


	// Constructor
	public Point( double a, double b )
	{
		setPoint( a, b );
		System.out.println("Point constructor:" + this);
	}

	protected void finalize()
	{
		System.out.println("Point finalizer:"+this);
	}


	// Set x and y coordinates of Point
	public void setPoint( double a, double b )
	{
		x = a;
		y = b;
	}

	public void draw(Graphics g)
	{
		int nx = (int) (x*SCALE);
		int ny = (int) (y*SCALE);
		g.fillOval(nx-5,ny-5,11,11);
	}   

   // convert the point into a String representation
   public String toString()
      { return " (" + x + ", " + y + ")"; }
}


Maintained by John Loomis, last updated 6 June 2000