OOP – Inheritance (basic)

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

OOP – Inheritance (basic)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is inheritance?
A A child class gets methods from a parent class
B Copying a file
2. class Dog(Animal): what is Animal?
A The parent (base) class
B The child class
3. What does super() do?
A Creates a new class
B Accesses the parent class
4. If Animal has speak(), does Dog get it?
A No must redefine
B Yes automatically through inheritance
5. Can Dog override Animal's speak()?
A Yes define speak() in Dog
B No parent methods are locked

Match the Pairs

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

Left columnRight column
1class Dog(Animal) AError: super() takes no arguments in Python 3
2super(Animal).__init__() BOk: Calls the parent class constructor
3class Dog(Animal): COutput: True
4super().__init__() DOutput: True
5issubclass(bool, int) EValid: Dog inherits from Animal
6isinstance(True, int) FError: Missing colon at end of class definition

Definitions

Inheritance

Fill in the Blanks

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

1.
class Dog(______): pass
2.
class Dog(Animal): def ________(self, name, breed): _____().________(name)
3.
class Dog(Animal): def speak(self): return self.name + ' says Woof!' # this _________ the parent
4.
dog = Dog('Rex') print(__________(dog, Animal))
5.
class Circle(Shape): def ________(self, color, radius): _____().________(color)

Spot the Error

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

Question 1
A class Dog(Animal):
B def __init__(self, name, breed):
C self.name = name
D self.breed = breed
Question 2
A class Dog(Animal):
B def __init__(self, name, breed):
C super().__init__()
D self.breed = breed
Question 3
A class Dog extends Animal:
B def __init__(self, name):
C super().__init__(name)
D def bark(self):
E return 'Woof!'
Question 4
A class Dog(Animal):
B def __init__(self, name):
C super.__init__(name)
Question 5
A class Animal:
B def speak(self):
C return 'Sound'
D
E class Dog(Animal):
F def speak():
G return 'Woof'

Answer Key

OOP – Inheritance (basic)
Section A: Quick Questions
Q1
A — A child class gets methods from a parent class
Q2
A — The parent (base) class
Q3
B — Accesses the parent class
Q4
B — Yes automatically through inheritance
Q5
A — Yes define speak() in Dog
Section B: Match the Pairs
Q1
class Dog(Animal) → Error: Missing colon at end of class definition
Q2
super(Animal).__init__() → Error: super() takes no arguments in Python 3
Q3
class Dog(Animal): → Valid: Dog inherits from Animal
Q4
super().__init__() → Ok: Calls the parent class constructor
Q5
issubclass(bool, int) → Output: True
Q6
isinstance(True, int) → Output: True
Section C: Definitions
Q1
Inheritance — A child class receives methods and attributes from a parent class.~~Parent class
Section D: Fill in the Blanks
Q1
class Dog(Animal): pass
Q2
class Dog(Animal): def __init__(self, name, breed): super().__init__(name)
Q3
class Dog(Animal): def speak(self): return self.name + ' says Woof!' # this overrides the parent
Q4
dog = Dog('Rex') print(isinstance(dog, Animal))
Q5
class Circle(Shape): def __init__(self, color, radius): super().__init__(color)
Section E: Spot the Error
Q1
C — Should call super().__init__(name) to use the parent's initialization
Q2
C — Parent's __init__ expects name must pass it: super().__init__(name)
Q3
A — Python uses parentheses for inheritance, not 'extends' (that's Java)
Q4
C — super needs parentheses: super().__init__, not super.__init__
Q5
F — Override method needs self parameter too