Object-Oriented

Class

A class is a blueprint for creating objects. It defines what data an object holds and what it can do — all in one place.

Classes are the main building block of object-oriented programming. Instead of writing all your code in one file, you split it into classes — each one responsible for one thing.

Defining a Class#

A class has a name and a body. The body contains the data and behaviour that belong to it. The exact syntax depends on the language — see the language-specific pages below.

Constructor

A constructor is a special method that runs automatically when an object is created from a class. Its job is to set up the object’s initial state.

  new Player("Alice")
  ┌─────────────────────┐
  │     constructor     │  ← runs automatically
  │  sets name = Alice  │
  └─────────────────────┘
  Player object ready

What a Constructor Does#

When you create an object, you often need to give it some starting values — a name, a score, a position. The constructor receives those values as arguments and stores them on the object.

Field

A field is a variable that belongs to an object. It holds a piece of data that describes the object’s state — its name, its score, its position.

Each object created from a class has its own copy of the fields defined in that class. One Player object might have Name = "Alice" and another might have Name = "Bob" — they share the same class but hold different values.

Method

A method is a function that belongs to a class. It defines what an object can do.

Where a plain function stands on its own, a method is always attached to a class. When you call a method, you call it on a specific object — and the method can work with that object’s data.

Defining a Method#

A method is defined inside the class body. It has a name, a return type, and a body.

Object

An object is a specific instance created from a class. The class is the blueprint; the object is the thing built from it.

You can create as many objects as you need from the same class. Each one is independent — it has its own copy of the class’s data, separate from every other instance.

  ┌─────────────────┐
  │  class Player   │  ← blueprint (defined once)
  └────────┬────────┘
           │ instantiate
     ┌─────┴──────┐
     ▼            ▼
 ┌────────┐   ┌────────┐
 │ Alice  │   │  Bob   │  ← objects (created at runtime)
 │ score:3│   │ score:1│    each has its own data
 └────────┘   └────────┘

Class vs Object#

This distinction confuses almost every beginner. Here’s the clearest way to think about it:

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.