Scribe logoScribeNotes & practice
Sign inStart free
  1. Notes
  2. ›IB Computer Science
  3. ›B3 Object-oriented programming
  4. ›B3.1 OOP with a single class

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.

  • 4 min read
  • 5 parts
  • 10 questions
  • Draft
Start a Quick 5 on this topic→Jump to the 10 questions
1/5A class is a template from which objects are made
  1. 1A class is a template from which objects are made
  2. 2Attributes store data and methods define behaviour
  3. 3A constructor gives each new object its starting values
  4. 4Dot notation reaches an object’s attributes and methods
  5. 5Separate objects from one class keep separate data
  6. Check your recall
  7. Practice: 10 questions
1

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.

Figure 1. The class lists attributes and methods once. Each object built from it holds its own attribute values.

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.

Common mistake

A class is not an object. The class is only the template, and no student exists until an object is created.

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 1 done

2

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

Define a class Student with attributes name and grade and a method show that outputs both.
  1. 1
    Start the class with its name: class Student.
  2. 2
    List the attributes: name and grade.
  3. 3
    Write method show, which outputs name and grade.
  4. 4
    End the class.

Answer: The class Student has two attributes and one method, show.

Exam tip

Inside a method, an attribute belongs to the object that called the method. Do not treat it as a separate variable.

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.

Part 2 done

Quick check

One question on the part you just read

Easy

Identify the term for a template that defines the attributes and methods of its objects.
Show answer and explanation

Answer: A · Worked explanation

A class is the template, and objects are built from it. An object is one built item, so it is the product rather than the template. A constructor is a special method that sets up a new object, and an attribute is a single piece of data in the class.
3

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

A constructor Student(n, g) assigns n to name and g to grade. State the attributes after s = new Student("Ana", 11).
  1. 1
    The call runs the constructor with n = "Ana" and g = 11.
  2. 2
    The constructor assigns n to name.
  3. 3
    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 3 done

4

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.

Common mistake

Writing show() alone does not say which object to use. Always name the object first, as in s1.show().

Key term

dot notation

Dot notation is the syntax object.name used to access an attribute or call a method of a specific object.

Part 4 done

Quick check

One question on the part you just read

Easy

A class Book stores title and pages and has a method summary. Identify the attribute.
Show answer and explanation

Answer: C · Worked explanation

An attribute is data that an object stores, and the title is such data. The summary is a method, which is a block of code. Book is the name of the class, and new Book is the call that creates an object, so neither is an attribute.
5

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

Class Counter has an attribute count and a method add, which increases count by 1. Its constructor sets count to 0. Determine a.count and b.count after: a = new Counter(), b = new Counter(), a.add(), a.add(), b.add().
  1. 1
    Both constructors run, so a.count is 0 and b.count is 0.
  2. 2
    a.add() runs twice, so a.count becomes 2.
  3. 3
    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.

Note read

Check your recall[14 marks]

6 written questions. Answer each one in your head or on paper, then open the mark scheme.

  1. 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. 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. 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. 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. 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. 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.

3.

Easy·State[1 mark]

State the main job of a constructor.
Show answer and explanation

Answer: B · Worked explanation

A constructor runs when an object is created and assigns starting values to the attributes. Removing objects, comparing objects and naming the class are not its job. Naming the class happens in the class definition, before any object exists, so the constructor does not do it.
4.

Medium·Identify[1 mark]

A constructor Pet(n) assigns n to the attribute name. Identify the statement that creates a Pet called Rex and stores it in p.
Show answer and explanation

Answer: D · Worked explanation

The variable that will hold the object goes on the left, and the constructor call with its value goes on the right: p = new Pet("Rex"). Putting Pet on the left makes the class a variable and p a constructor. Using dot notation calls a method on an existing object, which does not exist yet.
5.

Medium·Predict[1 mark]

Objects s1 and s2 are made from Student. Both have grade 11. A method changes s1.grade to 12. Predict s2.grade.
Show answer and explanation

Answer: A · Worked explanation

Each object holds its own attribute values, so changing s1 leaves s2 untouched and s2.grade stays 11. The value 12 assumes the objects share one grade. The value 23 would come from adding the two grades, which nothing in the program does, and 0 would mean the attribute was reset.
6.

Medium·Calculate[1 mark]

Counter objects a and b each start with count 0. The program runs a.add() once and b.add() three times, where add increases count by 1. Calculate a.count + b.count.
Show answer and explanation

Answer: B · Worked explanation

Each object keeps its own count, so a.count is 1 and b.count is 3, and the sum is 1+3=41+3=41+3=4. The value 3 is only b.count. The value 8 comes from assuming both objects share one count that reaches 4, so each reads 4. The value 1 is only a.count.
7.

Medium·Identify[1 mark]

A program calls s1.setGrade(12). Identify what the call changes.
Show answer and explanation

Answer: C · Worked explanation

Dot notation names s1, so the method runs on that object and changes only its own grade. It does not touch other Student objects, because each holds its own data. Calling a method does not rewrite the class or the constructor, which stay as defined.
8.

Hard·Determine[2 marks]

Class Account has a constructor Account(b) that sets balance to b, and a method deposit(x) that adds x to balance. The program runs: a = new Account(50), a.deposit(20), b = new Account(a.balance), b.deposit(5). Determine a.balance and b.balance.
Show answer and explanation

Answer: D · Worked explanation

After the deposit, a.balance is 50+20=7050+20=7050+20=70. The second constructor receives 70, so b starts at 70, and its deposit gives 70+5=7570+5=7570+5=75. The deposit to b does not affect a, because they are separate objects. The answer of 75 for both assumes that a and b share one balance.
9.

Hard·Explain[2 marks]

A program calls show() on its own, with no object named. Explain why this is a problem.
Show answer and explanation

Answer: A · Worked explanation

A method belongs to a class and works on the data of one object. Without an object, the program cannot tell whose name and grade to use. Dot notation supplies that object. A method is not an attribute, it does not need its own constructor call, and deleting the class would remove the method.
10.

Hard·Predict[2 marks]

The constructor of Student requires two values, name and grade. A program runs s = new Student("Ana"). Predict the most likely result in most languages.
Show answer and explanation

Answer: B · Worked explanation

The constructor is written to receive two values, so a call with one value does not match it, and most languages report an error. Objects always have all the attributes the class defines, so none can be left out. A constructor call creates one object, not two, and nothing supplies a grade automatically.

Keep going

Next topicB3.2 OOP with multiple classesPrevious topicB2.5 File processing

More in B3 · Object-oriented programming

  1. B3.1OOP with a single classNotes · 10 questions
  2. B3.2OOP with multiple classesNotes · 10 questions· HL only

All IB Computer Science topics →

Draft: written by Scribe and checked automatically, not yet by a subject reviewer.

Practice questions are original, written by Scribe and checked automatically for correctness.

Topic codes follow the official numbering. Not endorsed by IB Diploma Programme or any exam board.

On this page

0/5 read

  1. 1A class is a template from which objects are made
  2. 2Attributes store data and methods define behaviour
  3. 3A constructor gives each new object its starting values
  4. 4Dot notation reaches an object’s attributes and methods
  5. 5Separate objects from one class keep separate data
  6. Check your recall
  7. Practice: 10 questions
All coursesScribeHow it worksPricingPrivacyTerms

Notes and questions are written by Scribe in our own words. Topic codes follow the official course numbering so you can find your place. Not produced or endorsed by the IB, College Board, AQA, OCR, Pearson Edexcel or any exam board.

© 2026 Scribe