Lesson 25
Inheritance
A subclass inherits attributes and methods from a parent: class Dog(Animal). It gets everything Animal has.
Define a method with the same name in the subclass to override it. super() calls the parent version when you still need it.
Use inheritance for real "is a" relationships. A Dog is an Animal; an Engine is not a Car.
class Animal:
def speak(self):
return "..."
class Dog(Animal):
def speak(self):
return "Woof"Check your understanding
What does overriding a method do?
CHALLENGE
Create Animal with speak() returning "..." and Cat(Animal) overriding it to return "Meow". Print Cat().speak().
Practice →