polymorphism Example
This polymorphism example and the next few programs deal with fundamental aspects of object-orientation, at the programming level.Polymorphism, what does it mean? The prefix poly- means 'many', morph means 'form', and -ism tacked on the end signifies that polymorphism is 'the existence of many forms'.
There are countless things that take many forms. Shapes, for example, can be rectangular, circular, triangular, and so on. Consider the classes shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Polymorphism Example
import java.awt.*;
abstract class Shape {
Color col;
int x,y; // position
abstract void draw();
}
//========================================
class Rectangle extends Shape {
int w, h;
void draw() {
// method details
}
}
//=======================================
class Circle extends Shape {
int radius;
void draw() {
// method details
}
}
//=======================================
class GraphicDesigner {
void designMethod(Shape sh) {
// method details
}
public static void main(String[] args) {
GraphicDesigner gd = new GraphicDesigner();
Shape[] shapes = new Shape[2];
shapes[0] = new Rectangle();
shapes[1] = new Circle();
for(int i=0;i<shapes.length;i++) {
gd.designMethod(shapes[i]);
}
}
}
4-9 This is the definition of a Shape class. In recognition of its many forms, two example classes are derived from it here.
A shape is something that has color, position, and can be drawn, no matter what the shape. So these aspects of a shape are attended to in this definition.
But it makes no sense to create or draw an abstract shape object. The abstract modifier of the class and the draw method direct the compiler to ensure this is so.
11-16 This definition of Rectangle extends Shape. This means that it is a direct subclass of it.
The attributes of a shape, color and position, apply to all shapes. These are therefore defined in the superclass Shape and are inherited by each subclass.
A rectangle also has a width and height, so these are defined here where they are relevant.
18-23 This definition of Circle also extends Shape and therefore inherits color and position fields. It includes its own radius field.
The shapes we are interested in are drawn, so each subclass must define how its shape is drawn in a draw() method.
Because the Shape definition included an abstract definition of draw() with no body, the compiler checks that each subclass provides a proper definition for it.
25-29 A new class GraphicDesigner is defined that makes use of the Shape class.
We can imagine that its instance methods manipulate shapes by moving them around and changing their colors, and so on.
Importantly these instance methods need only refer to Shape objects, not instances of its subclasses.
A reference to a Shape object can reference an instance of any of its subclasses.
31-41 A main() method is provided here although it could have been provided elsewhere in some other class definition.
It creates an instance of GraphicDesigner and two specific shape objects that are assigned to a Shape array.
Here is the power of polymorphism. A shape instance can be used irrespective of which subclass it was constructed from! designMethod(), with its Shape parameter, will work with ANY shape.
