IB Computer Science B3.2: Fundamentals of OOP for multiple classes (HL only)

New IB Computer Science syllabus · first exams May 2027 · Theme B · Paper 2 · HL only · Python and Java

B3.2 is about how classes work together: sharing code through inheritance, behaving differently through polymorphism, and being built from other objects. It also covers the design patterns that solve common problems.

StatementTopicCommand term
B3.2.1InheritanceExplain / Apply
B3.2.2Polymorphism, e.g. method overridingConstruct
B3.2.3AbstractionExplain
B3.2.4Composition and aggregationExplain
B3.2.5Design patternsExplain

Key points

B3.2.1 Inheritance

  • A child class extends a parent class, reusing its code and adding to it or changing it.
  • Access modifiers decide what the child can use:
    • private: the class only
    • protected: the class and its subclasses
    • public: everywhere
    • default: Java package access

B3.2.2 Polymorphism

  • Dynamic polymorphism (method overriding): a subclass replaces a parent method, for example Dog.speak() and Cat.speak(). Which version runs is decided at runtime, based on the object's actual type.
  • Static polymorphism (method overloading): several methods with the same name but different parameters, for example add(int, int) and add(double, double). Which version runs is decided at compile time.

B3.2.3 Abstraction

An abstract class, such as Shape, can't be instantiated. Its abstract methods, such as area(), force every subclass (Circle, Rectangle) to provide its own version, giving all subclasses a common interface.

B3.2.4 Composition vs aggregation

  • Composition: the part can't exist without the whole, for example a heart and a human.
  • Aggregation: the part can exist independently, for example a book and a library.

B3.2.5 Design patterns

  • Singleton: only one instance can exist, for example a single settings manager.
  • Factory: a method creates objects without the caller needing to know the exact class.
  • Observer: a subject notifies all its observers when it changes. For example, a weather station updates a phone app and a website widget.

Exam-style question

Construct code demonstrating dynamic polymorphism, using method overriding, for two subclasses of an Employee class that each calculate pay() differently. [6]

Mark scheme

  • [1] a correct constructor
  • [2] per correctly overriding subclass
  • [2] correct polymorphic output when called through the parent type

The output is Amara 3000, then Ben 800.

Python

class Employee:
def __init__(self, name):
self.name = name
def pay(self):
return 0
 
class SalariedEmployee(Employee):
def pay(self):
return 3000
 
class HourlyEmployee(Employee):
def pay(self):
return 20 * 40
 
staff = [SalariedEmployee("Amara"), HourlyEmployee("Ben")]
for e in staff:
print(e.name, e.pay())

Java

public class Employee {
String name;
public Employee(String name) { this.name = name; }
public int pay() { return 0; }
}
 
public class SalariedEmployee extends Employee {
public SalariedEmployee(String name) { super(name); }
@Override
public int pay() { return 3000; }
}
 
public class HourlyEmployee extends Employee {
public HourlyEmployee(String name) { super(name); }
@Override
public int pay() { return 20 * 40; }
}
 
Employee[] staff = { new SalariedEmployee("Amara"), new HourlyEmployee("Ben") };
for (Employee e : staff) {
System.out.println(e.name + " " + e.pay());
}

Common mistakes that cost marks

  • Confusing overriding with overloading. Overriding is a subclass replacing a parent method; overloading is the same method name with different parameters in one class.
  • Trying to instantiate an abstract class.
  • Getting composition and aggregation the wrong way round. Ask: can the part exist on its own?

Frequently asked questions

Is B3.2 on the SL syllabus?
No. The whole of B3.2 is HL only.

Which design patterns do I need?
Singleton, factory and observer.


Go further