Class

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.

Class (C#)

A class in C# is a blueprint for creating objects, defined with the class keyword.

This page covers Class in C#. For a language-agnostic introduction, see Class.

Defining a Class#

public class Player
{
}

public is the access modifier — it means the class can be used from anywhere in the program. The class body sits between the curly braces.

By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding Fields and Properties#

A field stores data on the object. A property provides controlled access to that data.

Class (JavaScript)

A class in JavaScript is a blueprint for creating objects, defined with the class keyword.

This page covers Class in JavaScript. For a language-agnostic introduction, see Class.

Defining a Class#

class Player {
}

By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding a Constructor#

The constructor method runs automatically when an object is created. Use it to set the initial state.

class Player {
    constructor(name) {
        this.name = name;
    }
}

this.name = name stores the value on the object. this refers to the current instance.

Class (Python)

A class in Python is a blueprint for creating objects, defined with the class keyword.

This page covers Class in Python. For a language-agnostic introduction, see Class.

Defining a Class#

class Player:
    pass

pass is a placeholder that means “empty body”. By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.

Adding a Constructor#

In Python, the constructor is a special method called __init__. It runs automatically when an object is created. The first parameter is always self — a reference to the object being created.

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.