06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

414 <strong>Learning</strong> <strong>Processing</strong><br />

class Cat extends Animal {<br />

Cat() {<br />

super();<br />

}<br />

void bark() {<br />

println( " MEOW! " );<br />

}<br />

}<br />

Th e following new terms have been introduced:<br />

• extends —This keyword is used <strong>to</strong> indicate a parent class for the class being defined. Note that classes<br />

can only extend one class. However, classes can extend classes that extend other classes, that is, Dog<br />

extends Animal, Terrier extends Dog. Everything is inherited all the way down the line.<br />

• super( ) —Super calls the construc<strong>to</strong>r in the parent class. In other words, whatever you do in the<br />

parent construc<strong>to</strong>r, do so in the child construc<strong>to</strong>r as well. This is not required, but is fairly common<br />

(assuming you want child objects <strong>to</strong> be created in the same manner as their parents). Other code can<br />

be written in<strong>to</strong> the construc<strong>to</strong>r in addition <strong>to</strong> super( ) .<br />

A subclass can be expanded <strong>to</strong> include additional functions and properties beyond what is contained in<br />

the superclass. For example, let’s assume that a Dog object has a hair color variable in addition <strong>to</strong> age,<br />

which is set randomly in the construc<strong>to</strong>r. Th e class would now look like so:<br />

class Dog extends Animal {<br />

color haircolor;<br />

Dog() {<br />

super();<br />

haircolor = color(random(255));<br />

}<br />

void bark() {<br />

println( " WOOF! " );<br />

}<br />

}<br />

Note how the parent construc<strong>to</strong>r is called via super( ) , setting the age <strong>to</strong> 0, but the haircolor is set inside<br />

the Dog construc<strong>to</strong>r itself. Suppose a Dog object eats diff erently than a generic Animal. Parent functions<br />

can be overridden by rewriting the function inside the subclass.<br />

class Dog extends Animal {<br />

color haircolor;<br />

}<br />

Dog() {<br />

super();<br />

haircolor = color(random(255));<br />

void eat() {<br />

// Code for how a dog specifically eats<br />

}<br />

void bark() {<br />

println( " WOOF! " );<br />

}<br />

}<br />

super() means execute code found in the<br />

parent class.<br />

Since bark() is not part of the parent class,<br />

we have <strong>to</strong> defi ne it in the child class.<br />

A child class can introduce new<br />

variables not included in the parent.<br />

A child can override a parent<br />

function if necessary.

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

Saved successfully!

Ooh no, something went wrong!