OOP principle: inheritance

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

OOP principle: inheritance

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line makes Dog a child class of Animal?
A class Dog(Animal):
B class Animal(Dog):
2. Which statement describes the relationship that inheritance shows?
A Has-a: a Dog is an Animal
B Is-a: a Dog is an Animal
3. A child class is created from Parent. What does the child get automatically?
A The parent's attributes and methods
B Only the parent's class name
4. Which pair is a good fit for inheritance?
A Student inherits from Person
B Car inherits from Wheel
5. Which is the parent (super) class in: class Cat(Pet):
A Cat
B Pet

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1class Dog(Animal): ADog is a child (sub) class that inherits the attributes and methods of the parent class Animal
2class Car(Vehicle): pass BThe parent and child are the wrong way round. Dog is an Animal, so Dog should be the child: class Dog(Animal):
3Is-a relationship: a Student is a kind of Person, so Student is the child class Cclass Student(Person):
4Reuse of code: a child object can call a method written only in the parent class DCar reuses everything from Vehicle without writing any new code, because a Car is a Vehicle
5class Animal(Dog): EPython has no inherits keyword. The parent class must go in brackets: class Teacher(Person):
6class Teacher inherits Person: Fpet = Dog() pet.eat()

Definitions

class Child(Parent):

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
class Animal: pass class Dog(______):
2.
class Bird(Animal): ___ speak(self): print("Tweet")
3.
rex = Dog() print(__________(rex, Animal))
4.
class Dog(Animal): pass print(__________(Dog, Animal))
5.
class Animal: pass class Dog(Animal): pass class Puppy(___): pass

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def Animal:
B def eat(self):
C print('Eating')
D class Dog(Animal):
E pass
F Dog().eat()
Question 2
A class Shape:
B sides = 0
C class Circle(shape):
D round = True
E print(Circle.sides)
Question 3
A class Animal:
B def eat():
C print('Eating')
D class Cat(Animal):
E pass
F Cat().eat()
Question 4
A class Staff:
B def _init_(self, name):
C self.name = name
D class Teacher(Staff):
E pass
F t = Teacher('Ms Lee')
Question 5
A class Animal:
B legs = 4
C class Bird extends Animal:
D legs = 2
E print(Bird.legs)

Answer Key

OOP principle: inheritance
Section A: Quick Questions
Q1
A — class Dog(Animal):
Q2
B — Is-a: a Dog is an Animal
Q3
A — The parent's attributes and methods
Q4
A — Student inherits from Person
Q5
B — Pet
Section B: Match the Pairs
Q1
class Dog(Animal): → Dog is a child (sub) class that inherits the attributes and methods of the parent class Animal
Q2
class Car(Vehicle): pass → Car reuses everything from Vehicle without writing any new code, because a Car is a Vehicle
Q3
Is-a relationship: a Student is a kind of Person, so Student is the child class → class Student(Person):
Q4
Reuse of code: a child object can call a method written only in the parent class → pet = Dog() pet.eat()
Q5
class Animal(Dog): → The parent and child are the wrong way round. Dog is an Animal, so Dog should be the child: class Dog(Animal):
Q6
class Teacher inherits Person: → Python has no inherits keyword. The parent class must go in brackets: class Teacher(Person):
Section C: Definitions
Q1
class Child(Parent): — Inheritance: the child (sub) class reuses the parent (super) class's attributes and methods and can add or change its own, showing an is-a relationship.
Section D: Fill in the Blanks
Q1
class Animal: pass class Dog(Animal):
Q2
class Bird(Animal): def speak(self): print("Tweet")
Q3
rex = Dog() print(isinstance(rex, Animal))
Q4
class Dog(Animal): pass print(issubclass(Dog, Animal))
Q5
class Animal: pass class Dog(Animal): pass class Puppy(Dog): pass
Section E: Spot the Error
Q1
A — A parent class is created with the class keyword, not def. It should be 'class Animal:' so that Dog can inherit from it.
Q2
C — The parent class name in brackets must match exactly. Python is case sensitive, so 'shape' is undefined. It should be 'class Circle(Shape):'.
Q3
B — Methods in the parent class need self as the first parameter. Without it the inherited method fails when the child object calls it. It should be 'def eat(self):'.
Q4
B — The constructor needs double underscores on both sides: __init__. With _init_ it is just an ordinary method, so Teacher inherits no constructor. It should be 'def __init__(self, name):'.
Q5
C — Python does not use 'extends'. A child class names its parent in brackets: 'class Bird(Animal):'.