Search⌘ K
AI Features

Overriding and Super Functions

Explore how to override inherited methods in Python subclasses and use super() to reuse parent behavior. You’ll learn to extend __init__(), avoid duplicated setup code, preserve inherited features, and customize methods such as __repr__() for cleaner object design.

We'll cover the following...

Inheritance is great for adding new behavior to existing classes, but what about changing behavior? Our Contact class allows only a name and an email address. This may be sufficient for most contacts, but what if we want to add a phone number for our close friends?

Overriding method

We can do this easily by setting a phone attribute on the contact after it is constructed. But if we want to make this third variable available on initialization, we have to override the __init__() method. ...