The Java Program: Simple.java

  1 // Simple.java -- illustrate simple overriding of methods
  2 
  3 class Point  {
  4    protected float x,y;
  5    Point (float x, float y) { this.x=x; this.y=y; }
  6    @Override
  7    public String toString () {
  8       return String.format ("(%.2f,%.2f)", x, y);
  9    }
 10 }
 11 
 12 class Rectangle extends Point {
 13    protected float height, width;
 14    Rectangle (float x, float y, float h, float w) {
 15       super (x,y); height=h; width=w;
 16    }
 17    @Override
 18    public String toString () {
 19       return String.format ("(%.2f,%.2f;h=%.2f,w=%.2f)", x, y, height, width);
 20    }
 21 }
 22 
 23 class Circle extends Point {
 24    protected float radius;
 25    Circle (float x, float y, float r) {
 26       super (x,y); radius=r;
 27    }
 28    @Override
 29    public String toString () {
 30       return String.format ("(%.2f,%.2f;r=%.2f)", x, y, radius);
 31    }
 32 }
 33 
 34 class Simple {
 35    public static void main (String [] args) {
 36       final Point p = new Point (2.3f, 4.5f);
 37       final Rectangle r = new Rectangle (2.3f, 4.5f, 45.1f, 89.1f);
 38       final Circle c = new Circle (2.3f, 4.5f, 0.3f);
 39       final Point [] list = new Point [] {p, r, c};
 40       for (Point q: list) System.out.println (q.toString());
 41       for (Point q: list) System.out.println (q);  // same effect
 42    }
 43 }