A Bit on Super and Sub Classes

 

As we have talked about in class there are some important relationships that need to be examined between superclasses and their subclasses and vice-a-versa.  One thing that can be difficult is making sure you develop the superclasses before you develop your subclasses so that you can extend them appropriately.  You cannot extend something that doesn't exist.

First we will develop the Point class, then we will develop the Circle class and then we will test them.

Now the code for Point.java should be copied into a new directory.

// Point.java
// Setting a point

public class Point
{
protected int x, y;

// No argument constructor
public Point()
{
// implicit call to superclass constructor occurs here
setPoint(0,0);
}

// Constructor
public Point(int a, int b)
{
// implicit call to superclass constructor occurs here
setPoint(a,b);
}

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

// get x coordinate
public int getX()
{
return x;
}

// get y coordinate
public int getY()
{
return y;
}

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

This is very basic and is the basis of our next class.

 

Now we are going to extend this Point class into a subclass called Circle.

Now the code for Circle.java should be copied into a new directory.

// Circle.java

public class Circle extends Point
{
protected double radius;

// No argument constructor
public Circle()
{
// implicit call to superclass constructor
setRadius(0);
}

// Constructor
public Circle(double r, int a, int b)
{
super(a,b); // call to superclass constructor
setRadius(r);
}

// Set radius of circle
public void setRadius(double r)
{
if (r >= 0.0)
radius = r;
else
radius = 0.0;
}

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

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

// Convert the circle to a string
public String toString()
{
return "Center = " + "[" + x + ", " + y + "];" + "\nRadius = " + radius;
}
}

Now we have started getting more developed.

 

Now you want to copy the following code into a file called TestSubSuper.java in the same directory.  

// TestSubSuper.java

import java.text.DecimalFormat;
import javax.swing.JOptionPane;

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

p = new Point(30,50);
c = new Circle(2.7, 120, 89);

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;

output = 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 = output + "\n\nCircle c (via cirleRef): " + circleRef.toString();

DecimalFormat precision2 = new DecimalFormat("0.00");

output = output + "\nArea of c (via circleRef): " + precision2.format(circleRef.area());

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


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

System.exit(0);
}
}

Now you need to execute this java application.