// Point.java
// Definition of class Point
public class Point {
protected int x, y; // coordinates of the Point
public Point() { // No-argument constructor
// implicit call to superclass constructor occurs here
setPoint( 0, 0); }
public Point( int a, int b ) { // Constructor
// implicit call to superclass constructor occurs here
setPoint( a, b); }
public void setPoint( int a, int b ) // Set x and y coordinates of Point
{ x = a;
y = b; }
public int getX() {return x; } // get x coordinate
public int getY() {return y; } // get y coordinate
public String toString() // convert the point into a String representation
{ return "[" + x + ", " + y + "]"; }
} // end of class Point
----------------------------------------------
//Circle.java
// Definition of class Circle
public class Circle extends Point { // inherits from Point
protected double radius;
public Circle() // No-argument constructor
{ // implicit call to superclass constructor occurs here
setRadius( 0 ); }
public Circle(double r, int a, int b ) // Constructor
{
super( a, b); // call to superclass constructor
setRadius( r);}
public void setRadius( double r ) // Set radius of Circle
{ radius = ( r >= 0.0 ? r : 0.0 ); }
public double getRadius() { return radius; } // Get radius of Circle
public double area() { return Math.PI * radius * radius; } // Calculate area of Circle
public String toString() // convert the Circle to a String
{ return "Center = " + "[" + x + ", " + y + "]" + "; Radius = " + radius;}
} // end of class Circle
------------------------------------
//InheritanceTest.java
// Demonstrating the "is a" relationship
import java.text.DecimalFormat;
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(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; // assign subclass object to superclass variable pointRef
output += "\n\nCircle c (via pointRef): " + pointRef.toString();
// thus through polymorphism, the toString method that is invoked is the method of the
// subclass, not the method of the superclass
// If a program needs to perform a sub-class specific operation, such as area
// on a subclass object such as c referenced by a superclass variable such as pointRef,
// see the pointRef = c above, it must first cast the superclass variable to a
// subclass variable, a process known as downcasting
circleRef = (Circle) pointRef;
output += "\n\nCircle c (via circleRef): " + circleRef.toString();
DecimalFormat precision2 = new DecimalFormat( "0.00" );
output += "\nArea of c (via circleRef): " + precision2.format( circleRef.area() );
if ( p instanceof Circle ) { // Attempt to refer to Point object with Circle reference
circleRef = (Circle) p; // line 40 in Test.java
output += "\n\ncast successful";
}
else output += "\n\np does not refer to a Circle";
System.out.println("Demonstrating the \"is a\" relationship\n" + output);
System.exit( 0);
} // end of main method
} // end of class InheritanceTest