Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile 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 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.

Here's ShowShapes.java:



Note that to stop the program, you'll need to pull up the console you started it in and press control-C.


CodeBarnBeginnerJava
 
    Bookmark Topic Watch Topic
  • New Topic