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.

that a subclass has access to the superclass’s methods (<strong>and</strong> properties). It’s also possible<br />

for a subclass to override methods within the superclass. This is accomplished by creating<br />

methods in the subclass with identical signatures to methods in its superclass.<br />

In the last example, the following three instantiation lines<br />

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

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

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

create Creature, Ogre, <strong>and</strong> Elf objects (the Ogre <strong>and</strong> Elf objects are also of type<br />

Creature, through inheritance). The addCreature(Creature c) method in the Engine<br />

class includes a parameter of type Creature, allowing not only objects of type Creature to<br />

be passed to the method as arguments, but also any subclasses of Creature. Inside this<br />

method is a call, using the passed-in object reference, to the common castSpell()<br />

method. Here’s where the magic happens. In setup(), when I call the Engine<br />

addCreature() method three times:<br />

eng.addCreature(c);<br />

eng.addCreature(o);<br />

eng.addCreature(e);<br />

passing in Creature, Ogre, <strong>and</strong> Elf objects, respectively, the JVM automatically figures out<br />

which implementation of the castSpell() method to call, based on the passed-in argument.<br />

In the first call, I’m passing in an object of type Creature, so the castSpell() implementation<br />

in the superclass is used. But in the next two calls, the castSpell()<br />

implementations within the Ogre <strong>and</strong> Elf subclasses, respectively, are used. This is polymorphism<br />

in action <strong>and</strong> a powerful feature of <strong>Processing</strong> <strong>and</strong> Java.<br />

Polymorphism with interfaces<br />

At the risk of scaring readers away from the last few pages of this chapter, I<br />

feel it’s only fair to warn you that you’re about to head down a double black<br />

diamond trail (of the mind). This material is very advanced <strong>and</strong> presented in<br />

a condensed format. Read it at your own risk.<br />

OBJECT-ORIENTED PROGRAMMING<br />

Another variation on polymorphism in Java/<strong>Processing</strong> involves the use of interfaces. As I<br />

mentioned earlier, classes are data types, <strong>and</strong> a class that extends another class becomes<br />

of that secondary type as well. Interfaces are also legal data types—you can declare an<br />

object reference variable as an interface type, just like a class type. In the SportsGear<br />

interface discussion a few pages back, the Bicycle <strong>and</strong> Skis classes each implemented the<br />

SportsGear interface. Thus, instantiated Bicycle <strong>and</strong>/or Skis objects would also both be<br />

of the secondary type SportsGear. The Bicycle class also extended a Vehicle class, so it<br />

would actually be of three types: Bicycle, Vehicle, <strong>and</strong> SportsGear. In <strong>Processing</strong> <strong>and</strong><br />

Java, a class is only allowed to extend a single class, but it can implement as many interfaces<br />

as you’d like. This is why it is said that multiple inheritance of interface is permitted<br />

in <strong>Processing</strong>/Java.<br />

331<br />

8

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

Saved successfully!

Ooh no, something went wrong!