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.

Advanced OOP concepts<br />

Next, I’ll introduce some advanced OOP concepts, including encapsulation/information<br />

hiding, inheritance, composition, interfaces, <strong>and</strong> polymorphism. These concepts are fundamental<br />

to OOP, but they’re somewhat difficult to grasp <strong>and</strong> even harder to effectively<br />

apply. Underst<strong>and</strong>ing these concepts becomes increasingly important as your projects<br />

increase in size <strong>and</strong> complexity, especially when you want to create reusable code. If<br />

you’re a new coder (<strong>and</strong> are still recovering from the basic OOP whirlwind tour), you<br />

might want to skip this next section (for now). Just be sure to return to it, as you’ll definitely<br />

want to know about this stuff to fully leverage the power of <strong>Processing</strong> <strong>and</strong> Java.<br />

Fortunately, you can learn these concepts incrementally as you progress as a coder. It’s<br />

also possible, if you’re anything like me, that you may end up finding this stuff fascinating.<br />

Encapsulation <strong>and</strong> data hiding<br />

Encapsulation is one of the more “slippery” terms you’ll stumble across when you begin to<br />

learn programming. On the one h<strong>and</strong>, its meaning in programming is similar to its meaning<br />

in English—the encasing or enclosing of something. You can think about the way a<br />

function encases lines of code into an invokable unit as a form of encapsulation. One of<br />

the benefits of this arrangement is that the interface to a function <strong>and</strong> the implementation<br />

within the function are separated; this allows the implementation to change over time<br />

while still maintaining a consistent interface. For example, if I have a function called<br />

createPortrait() that initially draws a lousy stick-figure head, over time I can improve<br />

the algorithm to generate a more photorealistic head. The user, then, can just keep calling<br />

createPortrait() as the interface to the function remains constant, in spite of the<br />

improvements made to the guts of the function.<br />

In OOP, encapsulation refers to the encasing <strong>and</strong> grouping of an object’s data fields (properties)<br />

with its methods. As mentioned earlier, objects are created from classes, each with<br />

its own copies of the properties <strong>and</strong> methods defined in the class. In classic OOP, you don’t<br />

generally interact with an object’s properties directly. Instead, you use the object’s methods<br />

to get or set its property values. This principle is referred to as data or information<br />

hiding. In Java, for example, you normally use the keyword private when declaring properties<br />

to make them inaccessible. You declare methods (which are the structures that users<br />

of the class must use to interact with the properties) as public to get <strong>and</strong> set their values.<br />

For example, if I have class that has a name property, I would declare the property as private<br />

like this:<br />

private String name;<br />

And I would create public get <strong>and</strong> set methods to provide indirect access to the private<br />

name property, as follows:<br />

public String getName (){<br />

return name;<br />

}<br />

public void setName (String n){<br />

name = n;<br />

}<br />

OBJECT-ORIENTED PROGRAMMING<br />

319<br />

8

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

Saved successfully!

Ooh no, something went wrong!