IB Computer Science · B3 · Object-oriented programming
B3.1 OOP with a single class
Explain classes and objects, define attributes, methods and constructors, and create and use objects from a single class.
1/5A class is a template from which objects are made
Part 1 of 5: A class is a template from which objects are made
A school keeps a record for every student. Each record has the same fields, but the values differ. This is the idea behind classes.
A class is a template. It states what data its objects hold and what they can do. An object is one item built from that template.
Many objects can come from one class. Each object keeps its own data, so changing one record leaves the others untouched. Think of a class as a form and each object as a filled-in copy.
Key terms · tap one for its definition
class
A class is a template that defines the attributes and methods shared by every object created from it.
object
An object is a single instance of a class, holding its own values for the attributes the class defines.
Part 2 of 5: Attributes store data and methods define behaviour
Now look inside the template. An attribute stores a piece of data about an object, such as a student’s name. A method is a block of code that the object can run.
Methods often use the attributes. A method such as show outputs the object’s name and grade. A method such as setGrade changes the stored grade. Methods are what make an object more than a bundle of data.
Worked example
Define a class
- Start the class with its name: class Student.
- List the attributes: name and grade.
- Write method show, which outputs name and grade.
- End the class.
Answer: The class Student has two attributes and one method, show.
Key terms · tap one for its definition
attribute
An attribute is a variable defined in a class, of which each object holds its own value.
method
A method is a named block of code defined in a class that can use and change an object’s attributes.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: A · Worked explanation
Part 3 of 5: A constructor gives each new object its starting values
A new object needs values before it is useful. A constructor is a special method that runs once, at the moment an object is created. It receives values and assigns them to the attributes.
This is called initialising the object. Most languages name the constructor after the class or give it a fixed name. Without one, attributes may be empty until the program sets them. In the examples here, the keyword new calls the constructor.
Worked example
Use a constructor
- The call runs the constructor with n = "Ana" and g = 11.
- The constructor assigns n to name.
- The constructor assigns g to grade.
Answer: s.name is "Ana" and s.grade is 11.
A constructor can also check its values. For example, it can refuse a negative grade.
Key term
constructor
A constructor is a special method that runs when an object is created and assigns starting values to its attributes.
Part 4 of 5: Dot notation reaches an object’s attributes and methods
With the class and constructor written, the program can build and use objects. Three steps cover most cases.
- Create the object with the constructor: s1 = new Student("Ana", 11).
- Call a method with dot notation: s1.show().
- Read or change an attribute the same way: s1.grade.
Dot notation writes the object first, then a full stop, then the attribute or method. The object name tells the program whose data to use.
Key term
dot notation
Dot notation is the syntax object.name used to access an attribute or call a method of a specific object.

Quick check
One question on the part you just read
Easy
Show answer and explanation
Answer: C · Worked explanation
Part 5 of 5: Separate objects from one class keep separate data
Two objects from one class share a design but not their values. Changing an attribute of one object leaves the other unchanged. This is easiest to see in a trace.
Worked example
Trace two counters
- Both constructors run, so a.count is 0 and b.count is 0.
- a.add() runs twice, so a.count becomes 2.
- b.add() runs once, so b.count becomes 1.
Answer: a.count is 2 and b.count is 1.
Each object responds using its own attributes. If s1 and s2 both call show, the outputs differ because their attributes differ. That is why one method can serve every object.
A list can hold many objects. A loop can visit each one and call its method. This keeps the program short, even for hundreds of objects.
Check your recall[14 marks]
6 written questions. Answer each one in your head or on paper, then open the mark scheme.
- 1.Distinguish[2 marks]
Distinguish between a class and an object.
Show mark schemeHide mark scheme
Model answer
A class is a template that defines the attributes and methods. An object is one item created from the class, with its own attribute values.
- 1 mark for stating that a class is a template defining attributes and methods
- 1 mark for stating that an object is one item built from the class with its own values
- 2.Describe[3 marks]
Describe the purpose of a constructor.
Show mark schemeHide mark scheme
Model answer
A constructor is a special method that runs when an object is created. It receives values from the program. It assigns those values to the attributes, giving the object its starting state.
- 1 mark for stating that it is a special method that runs when an object is created
- 1 mark for stating that it receives values
- 1 mark for stating that it assigns the values to the attributes
- 3.Describe[3 marks]
Describe how a class defines attributes and methods.
Show mark schemeHide mark scheme
Model answer
The class gives its name and then lists its attributes, the data each object stores. It also lists its methods, blocks of code that use or change those attributes. The class ends after the last method.
- 1 mark for stating that the class is given a name
- 1 mark for stating that attributes are listed as the data each object stores
- 1 mark for stating that methods are written as blocks of code that use or change the attributes
- 4.Determine[2 marks]
A class Counter has an attribute count that its constructor sets to 0, and a method add that increases count by 1. Determine the value of x.count after: x = new Counter(), x.add(), x.add(), x.add().
Show mark schemeHide mark scheme
Model answer
The constructor sets x.count to 0. Three calls of add increase it by 1 each time, so x.count is 3.
- 1 mark for stating that the constructor starts count at 0
- 1 mark for obtaining x.count = 3
- 5.Write down[1 mark]
Write down the statement that calls the method show on the object stored in the variable s1.
Show mark schemeHide mark scheme
Model answer
s1.show()
- 1 mark for writing s1.show() with the object named first
- 6.Explain[3 marks]
Explain why two objects of the same class can hold different data.
Show mark schemeHide mark scheme
Model answer
Each object stores its own copy of the attributes. The constructor can give each object different starting values. Changing one object’s attributes does not change another’s.
- 1 mark for stating that each object has its own copy of the attributes
- 1 mark for stating that constructors can set different starting values
- 1 mark for stating that changing one object does not affect another

Practice this topic
10 quick-fire multiple-choice questions, easy to hard. Tap an answer to see if you're right and the worked explanation. (The written questions with mark schemes are under “Check your recall” above.)
0 right0 of 10 answered
Includes the 2 quick checks in the note above.
EasyState[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: D · Worked explanation
MediumPredict[1 mark]
Show answer and explanation
Answer: A · Worked explanation
MediumCalculate[1 mark]
Show answer and explanation
Answer: B · Worked explanation
MediumIdentify[1 mark]
Show answer and explanation
Answer: C · Worked explanation
HardDetermine[2 marks]
Show answer and explanation
Answer: D · Worked explanation
HardExplain[2 marks]
Show answer and explanation
Answer: A · Worked explanation
HardPredict[2 marks]
Show answer and explanation
Answer: B · Worked explanation