Computer Science Example
Inheritance and Polymorphism

import java.awt.*;
import java.applet.Applet;
import java.io.*;

/* An applet that reads data from a file, Figures.txt, that contains coordinates for circles and squares.  It stores these in an array of Figures.  Then it displays the figures on the applet using the paint method.  It uses inheritance and polymorphism. */

public class Figures extends Applet
{
    private FigureList figureList;
 
     public void init ()
     {
          figureList = new FigureList ();
          figureList.readList ();
     } // method init
 
     public void paint (Graphics g)
     {
          figureList.displayList (g);
     } // method paint
} // class Figures

// FigureList handles the array of Figures.
class FigureList
{
     private Figure [] list;
     private int listSize;
     final int maxSize = 30;
 
     FigureList ()
     {
          list = new Figure [maxSize];
          listSize = 0;
     } // constructor
 
     public void readList ()
     {
          Figure figure;
          try
          {
               BufferedReader figureFile = new BufferedReader (new InputStreamReader (new FileInputStream ("Figures.txt")));
               String type = figureFile.readLine ();
               while ((type != null) && (listSize < maxSize))
               {
                    if (type.equals ("Circle")) figure = new Circle (type);
                    else figure = new Square (type);
                    figure.readFigure (figureFile);
                    list [listSize] =  figure;
                    listSize ++;
                    type = figureFile.readLine ();
               }
               figureFile.close ();
          } catch (IOException e) {System.out.println ("IO Error");}
          catch (NumberFormatException e) {System.out.println ("Number Error.");}
     } // method readList
 
     public void displayList (Graphics g)
     {
          for (int count = 0; count < listSize; count ++)
               list [count].displayFigure (g);
     } // method displayList
} // class FigureList

// Figure is the super class that reads in the coordinates.
class Figure
{
     protected String type;
     protected int x, y;
     protected Color color;
 
     Figure (String t) { type = t; } // constructor
 
     public void readFigure (BufferedReader infile) throws IOException
     {
          x = Integer.parseInt (infile.readLine ());
          y = Integer.parseInt (infile.readLine ());
     } // method readFigure
 
     public void displayFigure (Graphics g)
     {
          g.setColor (color);
     } // method displayFigure
}// class Figure

/* The Circle class inherits the x and y coordinates from the Figure class.  It then reads in the diameter of the circle. */
class Circle extends Figure
{
     private int diameter;
 
     Circle (String type) { super (type); } // constructor
 
     public void readFigure (BufferedReader infile) throws IOException
     {
          super.readFigure (infile);
          diameter = Integer.parseInt (infile.readLine ());
          color = Color.red;
     } // method readFigure
 
     public void displayFigure (Graphics g)
     {
          super.displayFigure (g);
          g.fillOval (x, y, diameter, diameter);
     } // method displayFigure
} // class Circle

/* The Square class inherits the x and y coordinates from the Figure class and the reads in the length of the side. */
class Square extends Figure
{
     private int side;
 
     Square (String type) {super (type);} // constructor
 
     public void readFigure (BufferedReader infile) throws IOException
     {
          super.readFigure (infile);
          color = Color.green;
          side = Integer.parseInt (infile.readLine ());
     } // method readFigure
 
     public void displayFigure (Graphics g)
     {
          super.displayFigure (g);
          g.fillRect (x, y, side, side);
     } // method displayFigure
} // class Square