Red Hat – OOP & File Handling
advanced35 XP

Inheritance & Polymorphism

Extend classes and make objects behave differently

Inheritance

Python
class Animal:
    def __init__(self, name):
        self.name = name
    def speak(self): return "..."

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

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

for a in [Dog("Buddy"), Cat("Whiskers")]:
    print(f"{a.name}: {a.speak()}")

Same method name, different behaviour = polymorphism!

⌨️

Type it yourself

Don't just read it — type the example from above into the box, press Run ▶, and watch what happens. Typing it yourself is how it really sticks! Stuck? Tap Show example.

Python · Playground
Output
Press ▶ Run to see your code come to life…

Quick check

01/2

What does inheritance allow a child class to do?