B3.1.1Fundamentals of Object-Oriented Programming
0/9
B3.1 Fundamentals of OOP for a single classB3.1.1

Fundamentals of Object-Oriented Programming

Evaluate the fundamentals of OOP

SL + HL≈ 55 minCommand term: EvaluateFree lesson

In this lesson

  • Distinguishing classes from objects
  • Describing encapsulation, inheritance and polymorphism with real-world examples
  • Modelling real-world entities as classes with attributes and behaviours
  • Evaluating the advantages and disadvantages of OOP for different programs

LearnFrom variables to objects

    0 of 3

    CodeParallel lists go wrong

    names = ["Kiwi", "Puffin", "Toucan"]
    diets = ["Insects", "Fish", "Fruit"]
    del names[0] # a bird leaves the zoo: its name is removed...
    # ...but the matching diet is forgotten, so the lists no longer line up
    for i in range(len(names)):
    print(names[i], "eats", diets[i])
    Output
    Puffin eats Insects
    Toucan eats Fish

    Each bird’s details are split across two lists linked only by position. Removing from one list and forgetting the other silently pairs every bird with the wrong diet.

    CodeOne object per bird

    class Bird:
    def __init__(self, name, diet):
    self.__name = name
    self.__diet = diet
    def describe(self):
    return self.__name + " eats " + self.__diet
    birds = [Bird("Kiwi", "Insects"), Bird("Puffin", "Fish"), Bird("Toucan", "Fruit")]
    del birds[0] # the whole bird, name and diet together, is removed
    for b in birds:
    print(b.describe())
    Output
    Puffin eats Fish
    Toucan eats Fruit

    Bird is a class. Each new Bird(…) / Bird(…) creates an object that keeps one bird’s name and diet together, so they can never be separated.

    LearnClasses and objects

      0 of 4

      LabObjects in memory

      Create Player objects from the Player class, call their methods and assign variables. Watch the variables on the left and the objects they refer to.

      The Player class
      class Player:
      def __init__(self, name):
      self.__name = name
      self.__score = 0
      self.__lives = 3
      def addPoints(self, points):
      self.__score = self.__score + points
      def loseLife(self):
      if self.__lives > 0:
      self.__lives = self.__lives - 1
      def getScore(self):
      return self.__score
      Call a method on p1:
      Assign:p1 =

      Variables

      p1 → not created

      p2 → not created

      p3 → not created

      No objects yet: a class on its own is only a blueprint.

      • Not done: Create two different Player objects
      • Not done: Change one player’s score while another player keeps its own score
      • Not done: Make two variables refer to the same object, then change it through one of them

      LearnThree key ideas

        0 of 3

        CheckMatch the terms

        Select a term on the left, then its description on the right.

        CheckQuick check

        A program has a Student class and creates 30 student objects. How many sets of student attribute values are stored?

        LearnAdvantages and disadvantages of OOP

        AdvantagesDisadvantages
        Models real-world entities directly, so the design is easier to understand and discussFor a small, simple program, designing classes is extra work that brings little benefit
        A large problem is split into classes that can be written and tested separately, and by different people at the same timePrograms tend to be larger, and creating many objects uses more memory and can be slower
        Classes can be reused in other programs, and inheritance reuses code within a programIt takes time to learn, and badly designed class structures can be hard to follow and change
        Encapsulation protects an object’s data and lets a class’s internal code change without affecting the code that uses itSome problems, such as a single calculation, do not divide naturally into objects, and forcing them into classes makes the design awkward

        CheckIs OOP worth it?

        For which programs is an object-oriented design most worthwhile?

        1. A role-playing game with many characters, each with its own health, inventory and abilities
        2. A ten-line script that converts one temperature from Celsius to Fahrenheit
        3. A hospital system that manages patients, doctors, appointments and wards
        4. A one-off script that renames the photos in a folder by date
        5. A banking app maintained by a team for many years

        ExamWorked exam answer

        A music app stores songs. Distinguish between a Song class and a Song object.

        [2]
          Try writing your own answer first.

          ReviewMistakes examiners see

          Misconception

          Using “class” and “object” as if they meant the same thing.

          Misconception

          Describing encapsulation only as “hiding data”: it is also about keeping the data together with the methods that use it.

          Misconception

          Giving advantages of OOP without linking them to the program in the question.

          Exam tip

          In an Evaluate answer, weigh advantages against disadvantages for the specific program and finish with a justified conclusion.

          Exam practiceExam-style questions

          4 questions · 13 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.

          Scenario

          A wildlife park is replacing its spreadsheets with a program. It keeps records of about 400 animals of many species, their enclosures and their feeding times, and keepers update the records every day. The park expects the program to be extended for years to come.

          (a)Explain · AO3[2]

          The Animal class has a constructor that takes a name and a weight in kg, and the methods setWeight and getWeight. After the lines shown run, the program calls z1.getWeight(). State the value returned and explain why.

          z1 = Animal("Zuri", 820)
          z2 = z1
          z2 = Animal("Kito", 640)
          z2.setWeight(655)
          (b)Explain · AO3[3]

          Mammals, birds and reptiles are recorded in slightly different ways. Explain how inheritance could be used in the program.

          (c)Outline · AO2[2]

          Keepers ask each animal object for its daily care instructions. Outline how polymorphism would help here.

          (d)Evaluate · AO3[6]

          Evaluate the use of object-oriented programming for the wildlife park’s program.

          0 / 13

          0 of 4 questions marked. Mark every question to see your total.

          Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.