Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Preview

Object-oriented design

This is the first page of the lesson. Read it and have a go at the activities. The rest is in the full lesson.

50 minutes

A game with 100 enemies

You make a game. It has 100 zombies. Each zombie has a name, some health and a way to attack.

Do you write the same code 100 times? No. You write a plan once. Then you make 100 copies from the plan.

This is the big idea of object-oriented design. Object-oriented means a program is built from objects.

Key words

WordWhat it meansExample
ClassA plan for making objects. It says what they know and what they can do.Character
ObjectOne thing made from a class. Also called an instance.Sam, a Character
MethodA small named job that belongs to a class.take_damage()
AttributeA value that belongs to an object.health
EncapsulationKeep data and the code that uses it together, and protect the data.
Data abstractionShow only what the user needs. Hide the rest.
InheritanceA new class takes everything from an old class, then adds or changes things.
PolymorphismDifferent objects answer the same method call in their own way.
Design patternA tested answer to a problem that comes up again and again.

The cells students fill in are left blank in this preview.

A class is a plan

A class is like the plan for a house. You can build many houses from one plan.

Each house is an object. Each object has its own attributes, such as a name and health. Each object can do things. These things are methods.

Making an object from a class is called creating an instance.

Try it: a class and two objects

Code: python
class Character:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def speak(self):
        return self.name + " says: Hello"


sam = Character("Sam", 10)
ben = Character("Ben", 7)
print(sam.speak())
print(ben.health)

# Make a third Character and print what it says.

Quick check: class or object?

Have a go

Quick check: class or object?

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your answers are marked here and are not kept. A college account keeps every student’s marks.

The four characteristics

IdeaIn simple wordsReal life
EncapsulationData and its methods live together. Outside code cannot break the data.A bank machine: you press buttons, you cannot reach the cash
Data abstractionShow the simple view. Hide the details.A car pedal: you press it, you do not see the engine
InheritanceA child class gets everything from its parent class.A Zombie is a Character with a different voice
PolymorphismSame call, different result for each type.Say "make a sound" to a cat and a dog

The cells students fill in are left blank in this preview.

Try it: protect the health

Code: python
class Character:
    def __init__(self, health):
        self._health = health   # the _ means: keep out

    def take_damage(self, amount):
        self._health = max(0, self._health - amount)

    def health(self):
        return self._health


c = Character(10)
c.take_damage(50)
print(c.health())   # 0, never below 0

Try it: inheritance and polymorphism

Code: python
class Animal:
    def speak(self):
        return "..."


class Cat(Animal):
    def speak(self):
        return "Meow"


class Dog(Animal):
    def speak(self):
        return "Woof"


for pet in [Animal(), Cat(), Dog()]:
    print(pet.speak())

The lesson goes on

6 more parts of this lesson are in the full curriculum, with the tasks, the final check and the labs. A college account gives your students all of it.