04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Polymorphism<br />

Polymorphism sounds a lot worse than it really it is; it just means assuming multiple forms<br />

or shapes. Whether you realize it or not, you’re already very familiar with how polymorphism<br />

works. For example, imagine you <strong>and</strong> a friend decide to spend one Sunday building<br />

a birdhouse. You’d probably want some tools—say, a screwdriver <strong>and</strong> a saw. These tools<br />

don’t have much in common with one another except their general classification as tools.<br />

In OOP speak, we could say that each tool has two types: the type Tool <strong>and</strong> also its more<br />

specific type (Screwdriver or Saw). As you’re building the birdhouse, your friend is holding<br />

a screw <strong>and</strong> asks you to pass him a tool. Unless you’re a prankster, you pass him the<br />

screwdriver. Even though your friend didn’t specify which tool; the context determined<br />

which was appropriate. Also, since both the screwdriver <strong>and</strong> the saw are tools, there was<br />

no risk (when he asked for a tool) of you passing him a ham s<strong>and</strong>wich or whatever. This<br />

dynamic binding between a more general classification (tool) <strong>and</strong> a specific object (screwdriver),<br />

based on context, is precisely how polymorphism works in OOP.<br />

In technical terms, polymorphism in <strong>Processing</strong>/Java is based on the JVM’s ability to be<br />

able to link a method to a specific class’s implementation on the fly, making it possible for<br />

a well-designed program to actually exp<strong>and</strong> <strong>and</strong> grow over time, without necessarily going<br />

back into the source code. For example, let’s say you created a video game that included<br />

creatures who could each cast different magical spells. Using polymorphism, it would be<br />

possible to code a system that could h<strong>and</strong>le any number of creatures, including new ones<br />

you added after the main game engine was complete—without ever having to go back<br />

into the engine’s source code. Here’s a very simple code example of this scenario that you<br />

can run in <strong>Processing</strong>:<br />

// polymorphism example using inheritance<br />

void setup(){<br />

Creature c = new Creature();<br />

Ogre o = new Ogre();<br />

Elf e = new Elf();<br />

Engine eng = new Engine();<br />

eng.addCreature(c);<br />

eng.addCreature(o);<br />

eng.addCreature(e);<br />

}<br />

//superclass<br />

class Creature{<br />

void castSpell(){<br />

println("nothing to say");<br />

}<br />

}<br />

//subclass 1<br />

class Ogre extends Creature{<br />

void castSpell(){<br />

println("I miss ya donkey");<br />

}<br />

}<br />

OBJECT-ORIENTED PROGRAMMING<br />

329<br />

8

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!