Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
    Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic