B3 Object oriented programming

For the new IB Diploma Computer Science syllabus to start teaching in August 2025, and for first examinations in May 2027.

Unit and lesson overviews will be gradually published as developed.

Lesson 1: Introducing OOP

B3.1.1 Evaluate the fundamentals of OOP.

Lesson 2: Designing classes

B3.1.2 Construct a design of classes, their methods and behaviour.

Lesson 3: Instantiating objects

B3.1.4 Construct code to define classes and instantiate objects.

Lesson 4: Encapsulation

B3.1.5 Explain and apply the concepts of encapsulation and information hiding in OOP.

Lesson 5: Statics & non-statics

B3.1.3 Distinguish between static and non-static variables and methods.

Lesson 6,7,8: Programming scenarios

Exercise 1: Student and Course Enrollment System

Focus Classes: Student, Course

  1. Draw a UML class diagram for the Student class, including attributes for name, student ID, and year level.
  2. Implement the Python constructor __init__() based on the UML diagram.
  3. Make the student ID (student_id) a private attribute (using a double underscore).
  4. Implement a public method get_student_id() to allow controlled, read-only access to the private ID.
  5. Introduce a static attribute called __student_count (initialized to 0) to the class. This attribute should be incremented inside the constructor every time a new student object is instantiated.
  6. Add a static method called get_total_students() that returns the value of the __student_count.
  7. Implement the Course class with attributes like course_code, name, current_enrollment, and max_capacity.
  8. Add a non-static method is_full(self) that returns True if the course’s current enrollment is equal to the max capacity, and False otherwise.

Exercise 2: Library Item Management

Focus Class: LibraryItem

  1. Define LibraryItem with attributes for title, itemID, and a loan status attribute, __is_on_loan, which should be initialized to False in the constructor and kept private.
  2. Implement a public non-static method borrow_item() that sets __is_on_loan to True, but only if the item is currently available. It should print a message indicating success or failure.
  3. Implement a corresponding public non-static method return_item() that sets the status back to False.
  4. Add a non-static method display_status() that prints the item’s title and its current loan status (e.g., “Available” or “On Loan”). This uses the internal state but presents it publicly.
  5. Draw the complete UML class diagram for the LibraryItem class. Use correct notation for private and public attributes/methods.
  6. Add a static attribute and corresponding static method to track and report the total number of LibraryItem objects ever created (the library’s total inventory size).

Exercise 3: Point or Vector (Mathematical/Geometric)

Focus Class: Point2D

  1. Create the Point2D class with private attributes __x and __y, which are initialized via the constructor.
  2. Implement public getter methods (get_x(), get_y()) and public setter methods (set_x(new_x), set_y(new_y)) for the coordinates. This teaches the difference between direct attribute access and controlled method access.
  3. Implement a non-static method distance_from_origin(self) that calculates and returns the distance of this specific point from the origin (0, 0).
  4. Implement a static method create_origin() that returns a new Point2D object initialized at (0,0).

Lesson 9: Inheritance (HL)

B3.2.1 Explain and apply the concept of inheritance in OOP to promote code reusability.

Lesson 10: Polymorphism (overriding) (HL)

B3.2.2 Construct code to model polymorphism and its various forms, such as method overriding.

Lesson 11: Abstract classes (HL)

B3.2.3 Explain the concept of abstraction in OOP.

Lesson 12: Composition & aggregation (HL)

B3.2.4 Explain the role of composition and aggregation in class relationships.

Lesson 13: Design patterns (HL)

B3.2.5 Explain commonly used design patterns in OOP.

Lesson 14,15,16,17: Programming scenarios (HL)

Lesson 18,19: Exam style questions

Lesson 20: Assessment


Copyright © Paul Baumgarten.