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.

Inheritance<br />

In OOP, inheritance describes the ability of one class to inherit, or extend, the attributes<br />

<strong>and</strong> capabilities of another class. For example, if I create a class called Box, I could give it<br />

attributes such as height, width, depth, x, y, color, <strong>and</strong> so forth. It might also have methods<br />

to get <strong>and</strong> set these properties, <strong>and</strong> perhaps a create() method. I could then use this<br />

Box class as the base for another class or even a set of classes. A house, a cage, <strong>and</strong> a suitcase<br />

are all radically different objects, yet they each rely on a box structure. Instead of<br />

each of these classes defining their own height, width, depth, x, y, <strong>and</strong> color properties,<br />

they could inherit these from the Box class.<br />

In <strong>Processing</strong> <strong>and</strong> Java, when a class inherits form another class, the term extends is used<br />

to describe the relationship of one class inheriting from another. In addition, the term<br />

superclass is used to describe the class being extended from, <strong>and</strong> subclass is used to<br />

describe the class that extends the superclass. In the box example, Box would be the<br />

superclass <strong>and</strong> House, Cage, <strong>and</strong> Suitcase would each be subclasses of Box; it is also said<br />

that each of these subclasses extends the Box class. When a class extends a class, besides<br />

having access to the properties <strong>and</strong> methods in the superclass, the subclass can add its<br />

own additional properties <strong>and</strong> methods that would be unique to the class. For example,<br />

the House class, besides having the general properties of a box, might also have a roof<br />

property, a chimney property, <strong>and</strong> so on. There are some additional subtle <strong>and</strong> advanced<br />

concepts involved in inheritance, which I’ll look at in the next few chapters. However, for<br />

now, I’ve created a sketch that illustrates basic inheritance. The sketch reuses the linear<br />

<strong>and</strong> radial gradient examples from earlier in the chapter, with some minor modifications.<br />

Gradient class<br />

The first class I designed for this example was a Gradient class. This class will be the base<br />

class for a LinearGradient class <strong>and</strong> also a RadialGradient class. In other words, the<br />

Gradient class will be the superclass, <strong>and</strong> the LinearGradient <strong>and</strong> RadialGradient subclasses<br />

will extend the Gradient class. In addition, in thinking about these class relationships,<br />

I decided that I didn’t want people to be able to directly use the Gradient class (i.e.,<br />

instantiate the class), but rather only use it as a superclass for building other types of custom<br />

gradients. Java has a keyword (abstract) that is used to enforce this condition (which<br />

<strong>Processing</strong> has access to) that I have included in the Gradient class. Here’s the class<br />

(please note that this class, as well as the individual subclasses I’ll look at shortly, are not<br />

intended to be run independently):<br />

abstract class Gradient {<br />

// constants<br />

// these can't be changed<br />

final static int AXIS_VERTICAL = 0;<br />

final static int AXIS_HORIZONTAL = 1;<br />

// instance fields with default values<br />

// these can be changed<br />

color c1 = color(255);<br />

color c2 = color(0);<br />

Rectangle bounds = new Rectangle(0, 0, width, height);<br />

COLOR AND IMAGING<br />

469<br />

10

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

Saved successfully!

Ooh no, something went wrong!