Object-Oriented Programming Explained with a Piano

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 Piano class.

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, digital
  • numberOfKeys: usually 88, but some have 76 or 61
  • brand: 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 DigitalPiano class inherits from Piano but adds a unique changeSound method.

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 isTuned property 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 AcousticPiano and DigitalPiano use playNote(), 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?

Lydia Bandy Avatar

Published by Lydia Bandy

Harpist, Pianist, Award Winning Music Teacher, and Software Engineer🎼👨‍💻 Bridging music and technology with 25 years experience as a harpist, pianist, and award-winning instructor! Having 3-4 years as a software engineer and web developer, my goal is to teach coding and music theory to the world🌎🎵 Facebook/Instagram/YouTube: @LydiasPianoStudio

Leave a comment