Lesson 33 of 34

Inheritance: Building on a Class

10 min 35 XP

A class can inherit everything from another class, then add or change (override) things. No need to rewrite code.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

super() calls the parent class's method. It's most often used in __init__.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

The four pillars of OOP: Encapsulation, Inheritance, Polymorphism and Abstraction. Interviewers ask about them often!

Challenge

+35 XP

A Shape class is given. Write Rectangle(Shape) with __init__(width, height) and an area() method (width × height). Then write Square(Rectangle) whose __init__(side) calls Rectangle through super().

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.