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.

void setup(){<br />

size(400, 400);<br />

background(50);<br />

smooth();<br />

Polygon p = new Polygon(0, 0, 175, 175, 8);<br />

translate(width/2, height/2);<br />

p.create();<br />

}<br />

One final point: <strong>Processing</strong> <strong>and</strong> Java allow a superclass to be assigned an object reference<br />

to any class that has extended it or is below it in the inheritance lineage. Thus, an object of<br />

the Object class can be assigned an object reference to any class, because the Object class<br />

sits alone at the very top of the entire class lineage. So it is perfectly legal <strong>and</strong> common to<br />

write a statement like this:<br />

Shape myShape = new Polygon(10, 19, 233, 100, 8);<br />

Inheritance gives <strong>Processing</strong> <strong>and</strong> Java a lot of power, efficiency, <strong>and</strong> flexibility. Applying it<br />

judiciously, however, is not always so simple. One of the potential problems with inheritance<br />

is that classes get locked into dependencies. So, if I eventually decide to change a<br />

class for reuse, I may inadvertently mess up another program that depends, through inheritance,<br />

on a certain implementation within a superclass. This scenario <strong>and</strong> how to deal with<br />

it is a pretty advanced <strong>and</strong> subtle concern that won’t apply to most new coders, but it is<br />

something to keep in the back of your mind as you begin to develop larger <strong>and</strong> more<br />

complex projects.<br />

Composition<br />

OBJECT-ORIENTED PROGRAMMING<br />

One alternative (<strong>and</strong> also complimentary) approach to inheritance is called composition.<br />

Composition is another technique for developing relationships between classes. Composition<br />

is pretty straightforward to implement, <strong>and</strong> I think easier to get your head around than<br />

inheritance. Let’s imagine I’m trying to develop a virtual house. My house will be really<br />

simple; it will only contain windows. The windows, however, are pretty fancy. In OOP, it is<br />

virtuous to be able to modularize a construct into a class, encapsulating its properties <strong>and</strong><br />

behavior. Thus, it would be a good idea to create a Window class <strong>and</strong> have the class internally<br />

take care of creating <strong>and</strong> customizing the windows. Then, in my House class, I could<br />

just worry about where the windows are going to go <strong>and</strong> maybe how big to scale them; the<br />

Window class would take care of everything else. This is good OOP design. However, this<br />

approach also involves having multiple classes interacting with one other. The challenge of<br />

this inter-class communication is that unless there is an inheritance relationship, an object<br />

from one class can’t effectively communicate with an object of an unrelated class. In my<br />

previous inheritance example, the Polygon class extended the Shape class. This made<br />

sense because a polygon is a type of shape. Can I do the same thing with the House <strong>and</strong><br />

Window? Not really, as a window is not a type of house. Thus inheritance doesn’t conceptually<br />

seem like the right choice.<br />

323<br />

8

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

Saved successfully!

Ooh no, something went wrong!