Object-Oriented Programming

Object-oriented programming is a way of organising code around self-contained building blocks called objects. Each object is responsible for one thing — its own data and its own behaviour — instead of spreading that responsibility across the entire program.

Why It Matters#

As programs grow, keeping all the code in one place becomes unmanageable. A single file with hundreds of lines is hard to read, hard to change, and easy to break.

Object-oriented programming solves this by splitting code into smaller, focused pieces. Each piece handles one concern. Changes to one piece don’t ripple unpredictably through the rest.

The Main Building Blocks#

Class#

A class is a blueprint. It defines what an object looks like — what data it holds and what it can do. You define a class once and can create as many objects from it as you need.

Object#

An object is a specific instance created from a class. If a class is the blueprint for a house, an object is an actual house built from that blueprint. You can have many houses from the same blueprint, each with its own state.

Method#

A method is a function that belongs to a class. It defines what an object can do. A Player class might have a Play() method. A Game class might have a Run() method.

Field#

A field is a variable that belongs to an object. It holds the object’s state — its name, its score, its position. A Player object might have a Name field and a WinCount field. Different languages use different terms for this: Python calls them attributes, JavaScript calls them properties, but the concept is the same.

How It Fits Together#

  ┌─────────────────────────────┐
  │           Class             │  ← Blueprint (defined once)
  │                             │
  │  Properties  │  Methods     │
  │  (data)      │  (behaviour) │
  └──────────────┬──────────────┘
                 │ creates
        ┌────────┴────────┐
        ▼                 ▼
   ┌─────────┐       ┌─────────┐
   │ Object  │       │ Object  │  ← Instances (created at runtime)
   └─────────┘       └─────────┘

Common Mistakes#

Confusing a class with an object A class is the blueprint; an object is the thing built from it. Defining a Player class doesn’t create a player — you have to create an instance of it explicitly. Trying to use a class as if it were an object is a common early mistake.

Putting everything in one class Object-oriented programming only helps if each class has a clear, single responsibility. A class that handles the game rules, player input, and score display all at once defeats the purpose. When a class is doing too many things, it’s a sign it should be split.

Thinking OOP is always the right tool Object-oriented programming is powerful for organising large programs, but it adds structure that small programs don’t need. A five-line script doesn’t need classes. Use it when it genuinely helps organise the code — not as a rule that must always be followed.

Resources#