/en-tutor-prep-ib-cs-oop-single-class-uml-encapsulation/
/en-tutor-prep-ib-cs-oop-single-class-uml-encapsulation/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

IB Computer Science: OOP for a Single Class — Tutor Lesson Plan (classes, UML, static, encapsulation; a balance of 500 after 10 deposits and 3 withdrawals)

IB Computer Science: OOP for a Single Class — Tutor Lesson Plan (classes, UML, static, encapsulation; a balance of 500 after 10 deposits and 3 withdrawals)

  • 2026-11-28

Students can recite that a class is a blueprint and an object an instance, yet cannot write a class with a constructor and a private attribute. OOP is about design: one account class with 10 deposits and 3 withdrawals must end at 1,800, and an overdrawn withdrawal must be refused.

Objectives — the student can (1) separate the five concepts with an example each: a class is a blueprint, an object an instance, inheritance an is-a relation, encapsulation data plus methods in one unit, polymorphism one method name with different behaviour; (2) draw a UML class diagram for an account with 2 attributes and 3 methods, marking private with a minus and public with a plus; (3) explain static against non-static: with 3 accounts a class counter reads 3 while an instance variable shows 1 on every object; (4) write a constructor that sets the opening balance of 500 and trace 10 deposits of 250 and 3 withdrawals of 400 to a balance of 1,800; (5) protect state through encapsulation: a direct write to a private attribute fails, and a withdrawal of 2,000 is refused because it is 200 short, leaving the balance unchanged; (6) explain why encapsulation protects the integrity of an object and give a case that goes wrong without it.

01 Prerequisites (3-minute check)

If any of these fails, fix it before the lesson starts.

Skill Quick check Pass mark
Variables and methods a function has an input and a result, what are they called? parameters and a return value
Percentages what is 2% of 1,800? 36
Conditions a balance of 1,800 must cover a withdrawal of 2,000, what test is needed? refuse when the amount exceeds the balance

02 Lesson flow (60 minutes)

Time Stage Tutor does Student does
0-8 min Opening check what separates a class from an object? one sentence each
8-22 min Five concepts and trade-offs one everyday example per concept fill the concept table
22-36 min Class design and UML 2 attributes, 3 methods draw the class diagram
36-48 min Constructors and static 3 objects, 3 against 1 write class and constructor
48-57 min Encapsulation and hiding balance 1,800, withdraw 2,000 refused add private and the guard
57-60 min Exit check three questions marked on the spot work alone

03 Teaching script (read it out)

Scene 1: Five concepts: blueprint, instance, and three relations

Open with an account: the bank has one idea of an account, but every customer holds a separate one. The class is the shared description, with an owner, a balance, a deposit and a withdrawal; the object is the concrete one, a named customer with a balance of 1,800. Ask for the sentence a class has no balance, only objects do, which settles the pair at once.
Add the other three. Inheritance is an is-a relation: a savings account is an account with an extra interest rate and everything else unchanged; B3.1 asks only for the relation and its benefit (reuse of the parent code, one shared interface), since the code is B3.2. Encapsulation puts attributes and the methods that change them in one class, so the outside sees only methods; polymorphism is one method name with different behaviour, so withdraw might enforce a minimum balance on a savings account and allow an overdraft on a current account.
Give advantages and costs in pairs. Three advantages: it matches the real world, so people can discuss it; a change stays inside the class thanks to encapsulation; a new class reuses an old one through inheritance. Three costs: a small program carries extra wrapping, many objects cost memory and creation time, and deep inheritance chains are hard to follow because a method may be defined several levels up. Ask for one sentence on when not to use OOP: when the data and the operations are trivial and the program runs once, a few procedural lines are clearer.

Student task: take student, teacher and course, say which are classes and which are objects, then give a student two attributes and two methods.

Scene 2: Class design and UML: fix the interface first

Start from the requirement: an account records an owner and a balance and offers deposit, withdraw and a balance query. That is 2 attributes and 3 methods, and the UML diagram comes before any code. A UML class box has three compartments: the name, the attributes, the methods. Write an attribute as visibility name: type and a method as visibility name(parameters): return type. A minus is private and a plus is public, and those two symbols are all this chapter needs.
Have the class write balance as minus balance: decimal, getBalance as plus getBalance(): decimal, and deposit as plus deposit(amount: decimal): void. The commonest slip is a plus on balance: public means any code can overwrite the balance and the encapsulation is gone.
Then the three design tests: an attribute is what the object must remember, a method is what the outside asks the object to do; a balance query is a method rather than a raw read because a log or a permission check may be added later; methods take verbs and attributes take nouns, which is what a marker notices first.
Finally map the diagram back to code: the class name after class, the attributes as the variables a constructor sets, the methods as def or public void, one compartment per line. Ask for the sentence UML is the contract at design time and the code only copies it, which is the B3.1.2 marking point.

Student task: draw a UML class diagram for a library book with at least two attributes and two methods, one attribute private.

Scene 3: Constructors, static and encapsulation: the object guards its own state

Write the constructor first: opening an account needs an owner and an opening amount, so new Account(“customer”, 500) leaves balance at 500, not 0 and not undefined. Ask for the purpose: a constructor sets the state an object is born with, and note that once a constructor with parameters exists, new Account() fails to compile because the default constructor is gone.
Now trace a full run: an opening of 500, 10 deposits of 250 for 2,500, 3 withdrawals of 400 for 1,200, so the balance is 500 plus 2,500 minus 1,200, which is 1,800. Make the class write each step in a middle column rather than jumping to the answer; this is the standard trace the state of the object question.
Use a counter for static: the class variable accountCount records how many accounts were opened, so 3 objects give 3; write it as an ordinary instance variable and every object adds only its own, so all 3 show 1. Ask for the test: does this value belong to the class as a whole or to one object? Whole-class values such as a count, a rate or a constant are static; per-object values such as a balance or an owner are instance variables.
Finish with encapsulation. balance is private, so the outside reaches it only through deposit, withdraw and getBalance. Writing a.balance = 999999 is a compile error in Java and merely adds a second attribute in Python; teach both, because both languages are accepted. Withdrawing 2,000 from 1,800 is 200 short, so withdraw tests first, returns false and leaves 1,800 untouched. Ask for the sentence the rule lives inside the method, so the outside cannot go round it, which is exactly the value of information hiding: one copy of the rule, one place to change it.

Student task: add the insufficient-funds guard to withdraw, then write a three-step trace table proving the final balance is 1,800.

04 Question types and worked steps

Type Example Steps Answer
Tell a class from an object the account class and one customer account two questions: does it hold concrete values (then an object) and does it list shared attributes and methods (then a class) a class has no balance, an object does
Draw a UML class diagram an account with 2 attributes and 3 methods three steps: three compartments, a plus or minus on every member, types and return types written in minus balance; plus deposit, withdraw, getBalance
Write a constructor open an account with 500 three steps: define the constructor, assign the parameters to the attributes, instantiate and check the initial state the balance starts at 500, not 0
Trace object state 10 deposits of 250, 3 withdrawals of 400 three steps: state each method change, accumulate step by step, check the final and the intermediate values 500 + 2,500 – 1,200 = 1,800
Static against instance 3 accounts and a total counter two questions: does the value belong to the whole class (then static) or to one object (then an instance variable) static gives 3; an instance variable gives 1 each
Sanity check (teach this): Five checks: (1) the balance must equal opening plus deposits minus withdrawals: 500 plus 2,500 minus 1,200 is exactly 1,800, so any slip shows; (2) an overdrawn withdrawal must not go negative: 1,800 against 2,000 is 200 short, so the right result is a refusal with the balance held at 1,800, while minus 200 means the guard is missing; (3) a static count cannot be below the object count: 3 objects give 3, and 1 means an instance variable was written instead; (4) interest stays below the principal: 2% of 1,800 is 36.0, giving 1,836.0, so anything far above the principal is a percentage error; (5) the number of attributes matches the number of values the constructor sets: 2 attributes need 2 initial values. The commonest misjudgement is treating one created object as sharing a balance with the others: every object holds its own instance variables, and only static members are shared.

05 Independent practice (3 questions)

Question Answer What to watch
An account opens with 500, takes 10 deposits of 250 and 3 withdrawals of 400. (a) Final balance? (b) What is 2% interest on it? (a) 1,800; (b) 36.0, giving 1,836.0 with the principal add the deposits, subtract the withdrawals, then apply the rate to the final balance
The balance is 1,800 and the code wants to withdraw 2,000. (a) What should happen? (b) What happens without the guard? (a) refused, balance stays 1,800; (b) the balance becomes -200, i.e. 200 short the guard belongs inside the method so the outside cannot bypass it
3 accounts share a static counter. (a) What does it read? (b) What would an instance variable read on each object? (a) 3; (b) 1 on each static is shared by the class, an instance variable is one per object

06 Common mistakes and what to say

Mistake Student writes Say this
A public balance a.balance = 999999 from outside public hands the rule to the outside, so anyone can set 999,999. Keep the balance private and leave three doors: deposit, withdraw and getBalance.
Only a parameterised constructor new Account() fails once Account(name, opening) exists the default constructor is gone. Write a second one or always supply the arguments.
A static counter written as an instance variable 3 objects each showing 1 each object holds its own copy and adds only its own once. A whole-class count needs static: one copy shared by every object.
Deducting without a check balance 1,800 withdrawn by 2,000 gives -200 the method subtracts without asking whether the balance is enough, so the object enters an impossible state. Every state-changing method tests its condition first and refuses when it fails.

07 Homework (paper, section and question numbers)

08 Exit check (last 5 minutes)

Question Answer Decision
Concepts one example each of a class and an object a class with a balance means the pair is unclear
Tracing 10 deposits of 250, 3 withdrawals of 400 a reversed sign means add and subtract are swapped
Static a static counter over 3 objects 1 means it was written as an instance variable

09 Tutor FAQ

Does B3.1 require inheritance code?

No. B3.1 asks you to evaluate the five concepts: separate them, give benefits and costs, and supply one example each. Inheritance, polymorphism and abstract classes in code are B3.2, which is HL only.

Python has no private, so how is encapsulation taught?

Python uses a double leading underscore to signal do not touch, which is name mangling rather than a ban, but the exam wants the convention: the outside reaches the data only through methods. Answer that Java uses the private keyword and Python uses a double underscore plus a method interface, and both restrict direct access.

When is static the right choice?

Three cases: a count (how many accounts were opened, 3 giving 3), a shared constant (a rate or a limit), and a utility method that needs no object state. A balance or an owner, which differ per object, must be an instance variable.

Find a tutor — freeTutor Registration →

A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →