|
OO - polymorphism - wtf??
|
View this Thread in Original format
| UglyDave |
can some one explain what polymorphism is, as i'm having difficulty findin a good example on the net..
a class mate described it to me as being like this:
u have an alien, a lazer, and a ufo.
you tell them all to draw.
alien draws a pic of an alien.
lazer draws a lazer
ufo draws a ufo.
wtf?
please someone explain!
Dave |
|
|
| Kamikaze Badger |
Le Dictionare says:
| quote: | pol·y·mor·phism ( P ) Pronunciation Key (pl-môrfzm)
n.
1. Biology. The occurrence of different forms, stages, or types in individual organisms or in organisms of the same species, independent of sexual variations.
2. Chemistry. Crystallization of a compound in at least two distinct forms. Also called pleomorphism. |
|
|
|
| UglyDave |
| Le Dictionare is gay |
|
|
| Kamikaze Badger |
| Actually, I think the spelling is Dictionaire or something rather close to that. It's Francais. |
|
|
| UglyDave |
i used to go to dictionary.com but i realised it's e.
now:
google!
type:
define: word
sorted! |
|
|
| loconet |
| quote: | Originally posted by UglyDave
can some one explain what polymorphism is, as i'm having difficulty findin a good example on the net..
a class mate described it to me as being like this:
u have an alien, a lazer, and a ufo.
you tell them all to draw.
alien draws a pic of an alien.
lazer draws a lazer
ufo draws a ufo.
wtf?
please someone explain!
Dave |
That's actually a pretty good metaphor of what it is , here is a more formal explanation:
| quote: |
Polymorphism
Polymorphism is the ability of a class and possibly of different classes to have certain behavior identified by a common name but distinguished when invoked.
This is an extremely powerful feature of object-oriented programming. Consider two distinct classes in an inheritance hierarchy. Let us refer to their instances by their common class name and identify distinct behaviors by the same name. For example, let us ask all 'animals' of various species to 'draw' themselves. We use the common name (animal) and the common behavior (draw), but expect each instance to draw a diagram appropriate to its own species. We do not make separate drawing requests for each species. This enables us to introduce more species without introducing any more source code and to simplify our programming considerably.
Polymorphism means existence in many forms. This concept was first associated with programming by Strachey in 1967.
|
Source: http://cs.senecac.on.ca/~cszalwin/btp200/objec.html
Example from an ex prof of mine..:
code:
/** Explains polymorphism
JAC444 Lesson 4 Slide 11
@version 1.0, May 19, 2000
@author Jordan Anastasiade
*/
import java.util.*;
class Shape {
Shape(int i) {
System.out.println("\nShape constructor called");
}
void cleanup() {
System.out.println("Shape cleanup \n");
}
}
class Circle extends Shape {
Circle(int i) {
super(i);
System.out.println("\tDrawing a Circle \n");
}
void cleanup() {
System.out.println("\tErasing a Circle");
super.cleanup();
}
}
class Triangle extends Shape {
Triangle(int i) {
super(i);
System.out.println("\tDrawing a Triangle \n");
}
void cleanup() {
System.out.println("\tErasing a Triangle");
super.cleanup();
}
}
public class DrawSystem extends Shape {
private Circle c;
private Triangle t;
private Shape[] drawObjs;
DrawSystem(int nrObjs) {
super(nrObjs);
drawObjs = new Shape[nrObjs];
c = new Circle(1);
t = new Triangle(1);
drawObjs[0] = c;
drawObjs[1] = t;
System.out.println("DrawSystem constructor finishes.\n\n");
}
void cleanup() {
System.out.println("\n\nSystem.cleanup() begins");
for(int i = 0; i < drawObjs.length; i++)
drawObjs[i].cleanup();
super.cleanup();
}
public static void main(String[] args) {
DrawSystem x = new DrawSystem(2);
//drawing methods
x.cleanup();
}
}
Notice how the cleanup() is called and thanks to polymorphism, the corresponding cleanup from the derived objects are called. |
|
|
| GelatinPufF |
| quote: | Originally posted by UglyDave
can some one explain what polymorphism is, as i'm having difficulty findin a good example on the net..
a class mate described it to me as being like this:
u have an alien, a lazer, and a ufo.
you tell them all to draw.
alien draws a pic of an alien.
lazer draws a lazer
ufo draws a ufo.
wtf?
please someone explain!
Dave |
uuhhmm, not enough detail:nervous:
Think of it like this:
You have ANIMALS. (class Animal)
You have PETS.
PETS are ANIMALS. (class Pet extends Animal)
You have DOGS.
DOGS are PETS(class Dog extends Pet)
You have a POODLE.
POODLES are DOGS.(class Poodle extends Dog)
Now say we command them all to make a noise.
Animal.makeNoise(); //I don't think an undefined animal would make a noise;)
Pet.makeNoise(); //And I don't think an undefined pet would make a noise either;)
Dog.makeNoise(); //It would probably go "WOOF"
Poodle.makeNoise(); //It would probably go "eep!eep!"
See what's happening? A class is inheriting the properties of its super class:) And then USUALLY overiding them.
The dogs makeNose(); method returns "WOOF" while the poodles makeNoise(); method returns "eep!eep!" The classes have inherited their super classes method (makeNoise) but changed them. That's called polymorphism;) |
|
|
| UglyDave |
thank you everyone who has contributed to this thread!!!
GelatinPuff, i dunno what kinda crazy poodles you have :) |
|
|
| igottaknow |
| quote: | Originally posted by UglyDave
can some one explain what polymorphism is, as i'm having difficulty findin a good example on the net..
a class mate described it to me as being like this:
u have an alien, a lazer, and a ufo.
you tell them all to draw.
|
A better example would be:
A priest, a rabbi, and a nun walk into a bar... |
|
|
| GelatinPufF |
Well that was the only noise I could think of:nervous:
Plus I guess you're taking programming huh?:nervous:
You're in for some hard times you know that don't you?:nervous: |
|
|
| UglyDave |
| quote: | Originally posted by GelatinPufF
Well that was the only noise I could think of:nervous:
Plus I guess you're taking programming huh?:nervous:
You're in for some hard times you know that don't you?:nervous: |
..now that you're here... what's an abstract method? |
|
|
| igottaknow |
what's inheritance? and give an example
can you write me a program that does a bubble sort? |
|
|
|
|