Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
    Bookmark Topic Watch Topic
friki data migration
Ranch Hand
Posts: 772
  • Mark post as helpful
  • send pies
  • Report post to moderator

To learn more about polymorphism, mosey over to the campfire to hear about How my Dog learned Polymorphism

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

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,,, and 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.

    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!