home/ atoms/ processing-inheritance

Processing class inheritance lets subclasses extend and override their parent's behaviour

A class can extend another with the extends keyword, inheriting all the parent’s fields and methods. The subclass can add new fields, add new methods, or override inherited methods to change behaviour. super() calls the parent constructor; super.methodName() calls the parent version of an overridden method. This enables class hierarchies: a general Particle class handles physics, a specialised ArrowParticle extends it only to change display(). Inheritance supports polymorphism — an array of the parent type can hold instances of any subclass, and method calls dispatch to the correct subclass implementation.

Examples

class LimitedParticle extends Particle { float friction = 0.99; void update() { vx *= friction; vy *= friction; super.update(); limit(); } }

Assessment

Given a Particle class with x,y,vx,vy fields and update()/display() methods, write a FireworkParticle subclass that fades out over time by reducing an alpha field.

“class LimitedParticle extends Particle { float friction = 0.99; LimitedParticle(int ix, int iy, float ivx, float ivy, float ir) { super(ix, iy, ivx, ivy, ir);”
corpus · processing-handbook-no-login-mirror-pdf-reas-and-fry · chunk 99