B3.1.4 · Fundamentals of OOP for a single class
Defining classes and instantiating objects
Writing the blueprint, then bringing real objects into existence from it, with every attribute set correctly from the very first moment.
Explainer
Brackenhill Aquarium runs twelve display tanks. When a new tank is brought online, it is never registered half-configured: its reference code, its volume, and its target temperature are all recorded at the moment it enters the system. A tank sitting in the database with no temperature is not merely untidy, it is a tank nobody knows how to heat. Code has exactly the same requirement, and the constructor is what enforces it.
Defining a class means writing the blueprint: the attributes every object of that kind will hold, and the methods it will be able to perform. Defining a class creates no objects and reserves no data, it only describes what objects will look like.
Instantiation is creating a real object from that blueprint. Each instantiation produces a separate object in memory with its own attribute values, and returns a reference to it that can be stored in a variable.
# DEFINING the class: a blueprint, no tanks exist yet class Tank: def __init__(self, ref, litres, target_temp): self.ref = ref self.litres = litres self.target_temp = target_temp self.is_heated = False def describe(self): return f"{self.ref}: {self.litres}L at {self.target_temp}C" # INSTANTIATING: two real, separate tank objects reef = Tank("BR-01", 200, 25.0) coldwater = Tank("BR-02", 450, 14.0)
// DEFINING the class: a blueprint, no tanks exist yet public class Tank { private String ref; private int litres; private double targetTemp; private boolean isHeated; // the constructor: same name as the class, no return type public Tank(String ref, int litres, double targetTemp) { this.ref = ref; this.litres = litres; this.targetTemp = targetTemp; this.isHeated = false; } } // INSTANTIATING: two real, separate tank objects Tank reef = new Tank("BR-01", 200, 25.0); Tank coldwater = new Tank("BR-02", 450, 14.0);
Both languages do the same two jobs, with different spellings. Python names the constructor __init__ and takes self explicitly; Java names it after the class, declares no return type, and uses this. Java also needs new to instantiate, where Python just calls the class.
Explainer
What the constructor is actually for
A constructor is the special method that runs automatically the moment an object is created. Its job is to initialize the object's state: giving every attribute a starting value so the object is valid and usable from the instant it exists.
Notice the two different sources those starting values come from, because questions often ask for both:
Values passed in by whoever creates the object. ref, litres, and target_temp differ for every tank, so they arrive as parameters. The constructor's job is to store them on the object.
Values the class decides for itself. is_heated starts as False for every new tank, no matter who creates it, because a tank that has just been registered has not been switched on yet. Nobody passes this in; the constructor sets it.
A constructor does not just "create the object." Creation happens anyway. What the constructor adds is a guarantee: there is no moment at which a Tank exists with a missing reference or an unknown temperature, because the object is not handed back until every attribute has been set.
Explainer
Anatomy of self.ref = ref
This single line confuses more students than any other part of the statement, because the same word appears twice meaning two different things.
- self.refThe attribute: permanent storage belonging to this object. It survives after the constructor finishes and can be read later as
reef.ref. - refThe parameter: a temporary variable holding the value that was passed in. It disappears the moment the constructor finishes.
- =Copies the parameter's value into the attribute. Without this line the value arrives, is never stored, and is silently lost.
They are conventionally given the same name because they hold the same thing, but they are not the same variable. Writing ref = ref instead just reassigns the parameter to itself, and the object ends up with no ref attribute at all.
Worked example
What actually happens when you write Tank("BR-01", 200, 25.0)
Object creation looks like one instruction, but it is four distinct events. Step through them to see exactly when the object becomes usable.
Step 1. The call is made. The three arguments are evaluated. No object exists yet, and the variable reef has nothing in it.
Step 2. An empty object is created. Memory is set aside for one Tank, but none of its attributes have values. This half-built state is exactly what the constructor exists to eliminate, and it is why the object is not handed back yet.
Step 3. The constructor runs, with the new object as self. The parameters are bound to the values that were passed in, and each assignment copies one into the object.
Step 4. The class sets its own default. Nobody passed is_heated in. The constructor decides it, because every newly registered tank starts switched off. The object is now complete.
Step 5. A reference to the finished object is returned and stored. Only now does reef point at anything. Creating a second tank repeats all four events and produces a completely separate object.
Explainer
Default values in the constructor
Step 4 showed the class fixing a value nobody can override. There is a middle option too: a parameter with a default, which the caller may supply but does not have to.
# target_temp has a default; ref and litres do not class Tank: def __init__(self, ref, litres, target_temp=24.0): self.ref = ref self.litres = litres self.target_temp = target_temp tropical = Tank("BR-03", 180) # target_temp becomes 24.0 coldwater = Tank("BR-04", 450, 14.0) # target_temp becomes 14.0
Most Brackenhill tanks are tropical, so 24.0 is the sensible default and only the unusual tanks state a temperature. Parameters with defaults must come after those without, since Python matches arguments by position.
Required parameter (the caller must give it), parameter with a default (the caller may give it), or fixed inside the constructor (the caller cannot give it at all). Choosing correctly between the three is the design judgment this statement is really testing.
Try it yourself
Build your own tanks
The Brackenhill class, live and editable. Two things worth trying: delete the self.litres = litres line and see the error that appears only later, when something asks for it, and create a tank without its arguments to see what the constructor demands.
Running in offline fallback mode. The full Python engine couldn't load (blocked school network, or no internet), so this console switched automatically to a small hand-written engine. It runs this page's scenario, including constructors and default parameter values, but it isn't full Python: one visible difference is that whole-number floats print as 25 here rather than Python's 25.0.
Check your understanding
A student writes a constructor whose body is ref = ref instead of self.ref = ref. The program creates a Tank successfully, then crashes later when it calls reef.describe(). Why?
Common mistake
B3.1.4RecurringOn "construct a class" questions, the most common lost mark is a constructor that takes parameters but never assigns them to attributes, writing ref = ref rather than self.ref = ref. The code looks complete and creates an object without error, so candidates rarely spot it.
On written questions, describing the constructor's role as "it creates the object" is treated as too vague. The creditworthy point is that it initializes the object's attributes, giving the object a valid starting state.
Answer frame that scores: say when it runs (automatically, on creation) → say what it does (sets initial attribute values) → say why that matters (the object is never usable in an incomplete state).
What a weak answer looks like: “The constructor is used to create the object when the class is called.” It names the timing but not the job. Creation is not what earns the mark, initialization is.
What it should say: “The constructor runs automatically when a Tank is instantiated. It initializes the object's state by assigning the values passed in to attributes (self.ref, self.litres, self.target_temp) and setting is_heated to False, so no Tank can exist without a reference or a target temperature.” Timing, job, and consequence.
Revision questions
Past-paper style. Write an answer, then reveal the marking points and mark yourself.
[2]Outline the role of a constructor in initializing an object's state.
Marking points
评分要点
- A constructor runs automatically when an object is created.
- It takes the starting values as parameters and copies them into the object's attributes, so the object begins life in a complete, valid state.
- 构造函数在对象创建时自动运行。
- 它把初始值作为参数接收,并把它们复制到对象的属性中,使对象一开始就处于完整、有效的状态。
[4]Construct a class Enclosure with attributes for a reference code, an area in square metres, and a species name, including a constructor that initializes all three.
Marking points
评分要点
- class Enclosure: with a constructor def __init__(self, ref, area, species):.
- self.ref = ref (the reference code).
- self.area = area (area in square metres).
- self.species = species (the species name).
- All three attributes are assigned from parameters, so every Enclosure object is fully initialised on creation.
- class Enclosure:,构造函数为 def __init__(self, ref, area, species):。
- self.ref = ref(参考代码)。
- self.area = area(以平方米为单位的面积)。
- self.species = species(物种名称)。
- 三个属性都从参数赋值,所以每个 Enclosure 对象在创建时都被完全初始化。
[3]Explain the difference between the parameter ref and the attribute self.ref inside a constructor.
Marking points
评分要点
- ref is a parameter: a temporary local name that holds the value passed in, and it disappears when the constructor finishes.
- self.ref is an attribute: it belongs to the object and stores the value for the object's whole lifetime.
- The line self.ref = ref copies the passed-in value into the lasting attribute.
- ref 是形参:一个临时的局部名字,持有传入的值,构造函数结束时它就消失。
- self.ref 是属性:它属于对象,在对象的整个生命周期内存储这个值。
- self.ref = ref 这一行把传入的值复制到那个持久的属性中。
Flashcards
Active recall for this statement’s key terms.
Keep this statement’s practice with you
Free account: unlock every revision question and flashcard for B3.1.4, and every other IB CS 2027 statement.