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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

326<br />

There are many advantages for House <strong>and</strong> Window to have this special compositional relationship.<br />

By letting the house control when <strong>and</strong> where to draw the windows, it can ensure<br />

that the windows always fit, regardless of whether the house is resized or moved. By letting<br />

the windows take care of actually drawing themselves, the house only has to worry<br />

about the number of windows, their scale, <strong>and</strong> where they go. This is good, clean OOP in<br />

action.<br />

I realize that this is not easy stuff to grasp in its entirety, but I hope you are getting a sense<br />

of what OOP is <strong>and</strong> why it might be useful. The best way to learn OOP is to practice <strong>and</strong><br />

experiment writing classes/programs. You can read this stuff over <strong>and</strong> over, but until you<br />

apply it, it’s hard to get it to stick. The final concepts we’ll look at in this long chapter are<br />

not easy, so read at your own risk. If you choose to jump ahead, which is fine, do try to<br />

come back at some point.<br />

Interfaces<br />

In the example on inheritance a couple of pages back, a Polygon class extended a Shape<br />

class. This allowed the Polygon class to access the class members of the Shape class. This<br />

made sense because of the is-a relationship. A polygon is a shape. In the composition<br />

example, the House class contained a reference to the Window class through an instance<br />

variable of type Window. This was the right solution because a house has a window, but a<br />

window is not a house. I could have also used the Shape class as a base class for both the<br />

House <strong>and</strong> Window classes, as both a window <strong>and</strong> a house are shapes (of a sort). It is possible<br />

<strong>and</strong> actually very common to combine inheritance <strong>and</strong> composition when designing<br />

classes.<br />

However, there are times when you’ll have a class that has an is-a relationship to two or<br />

more classes. For example, a Bicycle class could have an is-a relationship to a Vehicle<br />

class <strong>and</strong> also a SportsGear class. Since the SportsGear class <strong>and</strong> the Vehicle class don’t<br />

have much in common, you can’t logically create an inheritance chain, where the Bicycle<br />

class extends the SportsGear class that extends the Vehicle class. The most logical solution<br />

is one of multiple inheritance, where the Bicycle class extends both the Vehicle class<br />

<strong>and</strong> the SportsGear class. In a language like C++, this could be done. However, in Java <strong>and</strong><br />

<strong>Processing</strong>, this is illegal, as the languages don’t support multiple class inheritance, which<br />

many people think is a good thing. If you want to learn more about this issue, check out<br />

www.javaworld.com/jw-12-1998/jw-12-techniques.html. However, Java/<strong>Processing</strong> does<br />

support another approach to multiple inheritance, often referred to as multiple inheritance<br />

of interface.<br />

Interfaces are class-like constructs that can only include method definitions <strong>and</strong> constants.<br />

Interfaces can’t include method implementations, the stuff between the curly braces.<br />

Method definitions include a return type (or the keyword void), the identifier (name) of<br />

the method, <strong>and</strong> the number <strong>and</strong> type of parameters. For example, method definitions in<br />

a SportsGear interface might include the following:<br />

String getBr<strong>and</strong>Name();<br />

float getUnitCost();<br />

void setUnitCost(float unitCost);<br />

void printWarranty();

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

Saved successfully!

Ooh no, something went wrong!