037
LVL 02 — CIRCUIT BREAKER SESSION 037 DAY 37

CLASS HIERARCHY

🎫 PIXELCRAFT-025
🔧 Refactor | 🟡 Medium | Priority: 🟡 Medium

Each filter has: name, range, default, value, apply(), reset(). Same structure 14 times. Create a class hierarchy.
CONCEPTS.UNLOCKED
🏗
Classes
class Filter { constructor() {...} apply() {...} } — blueprints for creating objects with shared structure and behavior. Define once, instantiate many times.
🔨
constructor, this, Methods
constructor() initializes the object. this refers to the instance. Methods are functions defined inside the class that operate on the instance's data.
🧬
Inheritance: extends
class BrightnessFilter extends Filter — inherits all properties and methods from Filter. Override only what's different. Shared behavior written once.
📞
super()
Calling the parent constructor: super('Brightness', -100, 100, 0). The child class passes arguments up to the parent's constructor. Must be called before using this.
Abstract Methods
The base class throws: throw new Error('apply() not implemented'). Forces every subclass to implement its own version. Forget to implement → clear error message.
Classes vs Functions
Classes for things with shared structure + state + behavior. Plain functions for stateless transformations. Not everything needs a class — practical guidelines matter.
HANDS-ON.TASKS
01
Create the Filter Base Class
class Filter { constructor(name, min, max, defaultValue = 0) { this.name = name; this.min = min; this.max = max; this.defaultValue = defaultValue; this.value = defaultValue; this.active = false; } apply(r, g, b) { throw new Error( `${this.name}: apply() not implemented` ); } reset() { this.value = this.defaultValue; this.active = false; } }
The base class defines the shared structure: every filter has a name, range, value, and can be reset. The apply() method throws — each subclass MUST override it with its own math.
02
Create Specific Filters
class BrightnessFilter extends Filter { constructor() { super('Brightness', -100, 100, 0); } apply(r, g, b) { return [ r + this.value, g + this.value, b + this.value ]; } } class GrayscaleFilter extends Filter { constructor() { super('Grayscale', 0, 1, 0); } apply(r, g, b) { if (!this.active) return [r, g, b]; const avg = (r + g + b) / 3; return [avg, avg, avg]; } }
Each filter is ~10 lines: constructor calls super() with its specific config, and apply() implements its unique math. All shared logic (reset, properties) is inherited for free.
03
Create a FilterManager
class FilterManager { constructor() { this.filters = [ new BrightnessFilter(), new ContrastFilter(), new SaturationFilter(), new GrayscaleFilter(), new InvertFilter(), ]; } applyAll(r, g, b) { let result = [r, g, b]; for (const filter of this.filters) { result = filter.apply(...result); } return result; } resetAll() { this.filters.forEach(f => f.reset()); } }
FilterManager doesn't know the details of any filter — it just calls apply() on each one. Adding a new filter = creating a class + adding one line to the array. Zero changes to FilterManager.
04
Refactor PixelCraft

Replace the 14 scattered filter functions with the class hierarchy. Wire the FilterManager into the rendering pipeline from Session 28.

05
When Are Classes Overkill?
Use Classes WhenUse Functions When
Shared structure + state + behaviorStateless transformation
Multiple instances neededSingle utility function
Inheritance makes senseComposition is simpler
Complex internal stateInput → output (pure)
06
Close the Ticket
git switch -c refactor/PIXELCRAFT-025-class-hierarchy git add src/scripts/ git commit -m "Refactor filters to class hierarchy with FilterManager (PIXELCRAFT-025)" git push origin refactor/PIXELCRAFT-025-class-hierarchy # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

Object-Oriented Programming (OOP).

Classes model "things" that have both state (properties) and behavior (methods). Inheritance shares common behavior while specializing.

// OOP dominated the 1990s–2000s:

Java     → everything is a class
C++      → classes + low-level control
C#       → classes + Microsoft ecosystem

// Modern JS uses a MIX of OOP
// and functional programming.

OOP         → "things" with state
Functional    → pure transformations

// Understanding BOTH lets you choose
// the right tool for each problem.
"Class Architecture"
[A] Add a SepiaFilter class that extends Filter. It should be ~10 lines. Test that it integrates with FilterManager without modifying FilterManager's code.
[B] Add a toString() method to Filter that returns "Brightness: +50" or "Grayscale: ON". Use it to build an "Active Filters" display in the UI.
[C] Research "composition over inheritance." Rewrite FilterManager using function composition instead of classes. Compare: which is more readable? More flexible?
REF.MATERIAL
ARTICLE
Mozilla Developer Network
Complete class reference: constructors, methods, static, private fields, inheritance, super(), getters/setters.
CLASSESMDNREFERENCE
VIDEO
Traversy Media
Practical OOP: classes, constructors, inheritance, method overriding, and when to use OOP vs functional approaches.
OOPPRACTICAL
ARTICLE
javascript.info
Interactive class tutorial: syntax, expressions, inheritance chain, and how classes relate to prototypes under the hood.
CLASSESINTERACTIVE
VIDEO
Fireship
Ultra-fast overview: encapsulation, abstraction, inheritance, polymorphism. The four pillars of OOP in 100 seconds.
OOPQUICK
ARTICLE
javascript.info
Extends, super(), overriding methods, constructor rules, and the prototype chain. Interactive exercises.
INHERITANCEINTERACTIVE
// LEAVE EXCITED BECAUSE
14 scattered functions became a clean class hierarchy. Adding a new filter is now a 10-line class. This is architecture.