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 />

328<br />

When I first learned about interfaces, by about this point in the discussion I got kind of<br />

annoyed. I was able to follow how to code interfaces, but I didn’t get the benefit. I mean,<br />

if you are forced to implement all the methods in the interface, why not just add the<br />

methods directly to the class to begin with? Wouldn’t this be less work <strong>and</strong> less confusing?<br />

What is the interface really giving you for this added effort? I think in retrospect that this<br />

was a good question. At this point, there is not much apparent benefit.<br />

Let’s now create another class that also implements the SportsGear interface, to see if you<br />

can begin to see the power of interfaces. (This code is not intended to be run either—I’ll<br />

get to that shortly.)<br />

class Skis implements SportsGear{<br />

//instance variables<br />

String br<strong>and</strong>Name;<br />

float unitCost;<br />

// constructor<br />

Skis(String br<strong>and</strong>Name, float unitCost){<br />

this.br<strong>and</strong>Name = br<strong>and</strong>Name;<br />

this.unitCost = unitCost;<br />

}<br />

// required implemented interface methods<br />

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

return br<strong>and</strong>Name;<br />

}<br />

float getUnitCost(){<br />

return unitCost;<br />

}<br />

void setUnitCost(float unitCost){<br />

this.unitCost = unitCost;<br />

}<br />

void printWarranty(){<br />

println("Skis: full replacement value for 5 years");<br />

}<br />

}<br />

In the Skis class, you can see that I included the same method definitions as in the<br />

Bicycle class. Notice also that I implemented the printWarranty() method a little differently<br />

in each of the two classes. I have the freedom to do this because the interface<br />

methods are empty. This is one of the benefits of using interfaces. They provide a shared<br />

interface for communicating with a group of related classes, but still allow a customized<br />

implementation of each of the common methods. As you get more experienced with<br />

coding, you’ll begin to recognize some recurring patterns in how related classes are<br />

designed, <strong>and</strong> even eventually be able to guess at some of the classes’ methods<br />

The customized implementation benefit I just mentioned is helpful, but still for my money,<br />

not quite enough to add so much complexity or extra work to my process. However,<br />

before you write off interfaces, there is another much more powerful benefit to using<br />

them that justifies the extra effort—polymorphism.

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

Saved successfully!

Ooh no, something went wrong!