This is an application, not an applet. Here is a UML class diagram to show what classes we will be working with:
We have an interface called "Shape". The classes Circle, Box and Poly (short for polygon) all implement the Shape interface. That means that they have agreed to implement all of the methods in Shape.
Here is the complete source code for Shape.java:
Here is the source for our three classes:
And to show off polymorphism in action, here is a program that creates a bunch of Circle, Box and Poly objects and stores them in a Shape array. Then it goes through the array and draws all of the shapes.
Go ahead and copy these programs to your disk as Shape.java, Cirle.java, Box.java, Poly.java and ShowShapes.java. Compile 'em up and run ShowShapes. You should see the beginnings of how most modern graphical operating systems work.
Note that to stop the program, you'll need to pull up the console you started it in and press control-C.