Think of a cookie cutter: not a cookie itself, but a template for making them. Press it into dough once and you have one cookie; press it a hundred times and you have a hundred — each distinct but sharing the same shape. A class is the programming equivalent: a blueprint for creating objects.
What is a class?
A class is a blueprint — a template that defines:
- Attributes (also called fields or instance variables): the data that each object will store. For a
Studentclass, attributes might bename,age, andyear_group. - Methods: the actions the object can perform, written as functions inside the class. For a
Studentclass, a method might beget_report()orsit_exam().
The class itself does not hold any specific data — it simply describes what kind of data each object will have and what kind of actions each object can take.
What is an object?
An object is a specific instance of a class — a single cookie made from the cutter. Each object has its own copy of the attributes defined in the class, holding its own specific values.
class Student:
def __init__(self, name, age, year_group):
self.name = name
self.age = age
self.year_group = year_group
def introduce(self):
print(f"Hello, I'm {self.name}, age {self.age}, in Year {self.year_group}.")
Creating objects (instances) from the class:
student1 = Student("Priya", 14, 9)
student2 = Student("Marcus", 15, 10)
student1.introduce() # Hello, I'm Priya, age 14, in Year 9.
student2.introduce() # Hello, I'm Marcus, age 15, in Year 10.
student1 and student2 are two separate objects, each with its own data, but both built from the same Student class.
What is __init__ and what is self?
__init__ (pronounced "dunder init") is a constructor — a special method that Python calls automatically whenever you create a new object. It sets up the object's initial attributes.
self refers to the object currently being created or used. When you write self.name = name, you are saying: "store the name argument as an attribute of this particular object." Every method in a class must have self as its first parameter, but you do not pass it explicitly when calling the method — Python handles that automatically.
What is the difference between attributes and methods?
| Feature | Attribute | Method |
|---|---|---|
| What it is | A variable belonging to the object | A function belonging to the object |
| How it is defined | self.name = name inside __init__ |
def method_name(self): inside the class |
| How it is accessed | student1.name |
student1.introduce() |
| Stores | Data (strings, numbers, lists, etc.) | Behaviour (instructions to carry out) |
How do you add more methods to a class?
Methods can read and modify an object's attributes:
class BankAccount:
def __init__(self, owner, balance):
self.owner = owner
self.balance = balance
def deposit(self, amount):
self.balance = self.balance + amount
print(f"Deposited £{amount}. New balance: £{self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance = self.balance - amount
print(f"Withdrew £{amount}. New balance: £{self.balance}")
else:
print("Insufficient funds.")
def show_balance(self):
print(f"{self.owner}'s balance: £{self.balance}")
Using the class:
account = BankAccount("Alice", 500)
account.deposit(200) # Deposited £200. New balance: £700
account.withdraw(100) # Withdrew £100. New balance: £600
account.show_balance() # Alice's balance: £600
What are the four pillars of OOP?
Object-oriented programming is built on four principles. You need to know all four for GCSE:
| Pillar | Meaning | Example |
|---|---|---|
| Encapsulation | Bundle data and methods together; hide internal details | A BankAccount object manages its own balance — external code cannot change it directly |
| Inheritance | A child class can inherit attributes and methods from a parent class | A SavingsAccount class inherits from BankAccount |
| Polymorphism | Different classes can share method names that behave differently | Both Dog and Cat have a speak() method, but the sound they make differs |
| Abstraction | Expose only what the user needs; hide how it works internally | You call deposit() without knowing how the balance is stored |
Frequently asked questions
What is the difference between a class and an object?
A class is the template (the cookie cutter); an object is a specific instance created from that template (an individual cookie). You can create many objects from a single class, each with its own attribute values.
Why use classes instead of just variables and functions?
Classes group related data and behaviour together, making large programmes far easier to understand, maintain, and extend. Instead of passing dozens of variables between functions, you pass a single object that carries all the relevant information about one entity.
Do I need to know classes for the GCSE computer science exam?
Yes. AQA, OCR, and Pearson all include object-oriented programming — including classes, objects, attributes, methods, and constructors — in their GCSE specifications. You should be able to write a class definition and create objects from it.
What does "instantiation" mean?
Instantiation is the process of creating an object from a class. When you write student1 = Student("Priya", 14, 9), you are instantiating the Student class to create the object student1.
Struggling to write your own classes? Visit aitutors.me — Professor Turing will guide you through building a class from scratch, attribute by attribute.