
If you’ve ever played or even seen a piano, you already understand the basics of Object-Oriented Programming (OOP). Let’s break it down:
1. Objects and Classes
In OOP, everything is an object, and objects belong to classes. A class is like a blueprint, while an object is an actual instance of that blueprint.
- Class:
Piano(defines what a piano is) - Object: A Yamaha grand piano, a Steinway upright, or a digital keyboard—each is an instance of the
Pianoclass.
2. Attributes (Properties)
Every piano has characteristics, just like objects in OOP. These are called attributes.
typescript class Piano {
type: string;
numberOfKeys: number;
brand: string;
constructor(type: string, numberOfKeys: number, brand: string) {
this.type = type;
this.numberOfKeys = numberOfKeys;
this.brand = brand;
}
}
type: grand, upright, digitalnumberOfKeys: usually 88, but some have 76 or 61brand: Yamaha, Steinway, Roland, etc.
3. Methods (Behaviors)
Pianos do things! In OOP, we call these methods—functions that objects can perform.
typescript class Piano {
playNote(note: string) {
console.log(`Playing ${note}`);
}
sustain() {
console.log("Sustaining sound...");
}
}
playNote(note): Plays a given note (C, D, E, etc.).sustain(): Holds the sound longer, like using a sustain pedal.
4. Inheritance (Extending the Blueprint)
Different pianos share common features but have unique differences. In OOP, we use inheritance to create specialized versions of a class.
typescriptclass DigitalPiano extends Piano {
hasSpeakers: boolean;
constructor(brand: string, hasSpeakers: boolean) {
super("Digital", 88, brand);
this.hasSpeakers = hasSpeakers;
}
changeSound(sound: string) {
console.log(`Changing sound to ${sound}`);
}
}
- The
DigitalPianoclass inherits fromPianobut adds a uniquechangeSoundmethod.
5. Encapsulation (Keeping Things Private)
Some details of a piano are hidden from the player, like how the hammer mechanism works. In OOP, we use encapsulation to restrict access to certain properties.
typescriptclass Piano {
private isTuned: boolean = true;
tune() {
this.isTuned = true;
console.log("Piano is now in tune.");
}
}
- The
isTunedproperty is private, meaning it can’t be changed directly.
6. Polymorphism (Same Method, Different Behavior)
A digital piano and an acoustic piano both play notes, but they do it differently. Polymorphism allows different objects to use the same method but behave in unique ways.
typescriptclass AcousticPiano extends Piano {
playNote(note: string) {
console.log(`Striking a hammer to play ${note}`);
}
}
class DigitalPiano extends Piano {
playNote(note: string) {
console.log(`Generating digital sound for ${note}`);
}
}
- Both
AcousticPianoandDigitalPianouseplayNote(), but one physically strikes strings while the other generates a digital sound.
Final Thoughts
Object-Oriented Programming organizes code the way a piano organizes music: into logical, reusable, and structured components. Whether you’re writing code or playing an instrument, it all comes down to patterns and creativity!
If you’re a musician learning to code, OOP might be more intuitive than you think! What other musical analogies can you think of?
