OOP – Inheritance (basic), online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
OOP – Inheritance (basic) in one minute
What students need to know
Dog gets all of Animal's methods and attributes automatically. super().__init__() sets up the parent's attributes in the child.
Inheritance lets a child class reuse all the code already written in the parent. The class in parentheses is the parent — Dog inherits from Animal.
Mistakes students make
- Should call super().__init__(name) to use the parent's initialization.
- Parent's __init__ expects name must pass it: super().__init__(name).
- Python uses parentheses for inheritance, not 'extends' (that's Java).
- super needs parentheses: super().__init__, not super.__init__.
PREVIOUS · MODULE 55OOP – Private Attributes (underscore convention)
YOU ARE HERE · MODULE 56OOP – Inheritance (basic)
NEXT · MODULE 57Enumerations – Basics
What students will learn in this module
Every question in Module 56
Quick questions
- What is inheritance?AA child class gets methods from a parent classBCopying a file
- class Dog(Animal): what is Animal?AThe parent (base) classBThe child class
- What does super() do?ACreates a new classBAccesses the parent class
- If Animal has speak(), does Dog get it?ANo must redefineBYes automatically through inheritance
- Can Dog override Animal's speak()?AYes define speak() in DogBNo parent methods are locked
Key terms
Students type the meaning of:
InheritanceParent classChild classsuper()
Match the pairs
class Dog(Animal)Match it to what it doessuper(Animal).__init__()Match it to what it doesclass Dog(Animal):Match it to what it doessuper().__init__()Match it to what it doesissubclass(bool, int)Match it to what it doesisinstance(True, int)Match it to what it doesFill in the blanks
class Dog(______): passclass Dog(Animal): def ________(self, name, breed): _____().________(name)class Dog(Animal): def speak(self): return self.name + ' says Woof!' # this _________ the parentdog = Dog('Rex') print(__________(dog, Animal))class Circle(Shape): def ________(self, color, radius): _____().________(color)
Spot the error
Question 1
Aclass Dog(Animal):B def __init__(self, name, breed):C self.name = nameD self.breed = breed
Question 2
Aclass Dog(Animal):B def __init__(self, name, breed):C super().__init__()D self.breed = breed
Question 3
Aclass Dog extends Animal:B def __init__(self, name):C super().__init__(name)D def bark(self):E return 'Woof!'
Question 4
Aclass Dog(Animal):B def __init__(self, name):C super.__init__(name)
Question 5
Aclass Animal:B def speak(self):C return 'Sound'DEclass Dog(Animal):F def speak():G return 'Woof'
