Search⌘ K
AI Features

Subclasses

Explore how Python subclasses inherit attributes and methods from their superclasses. Understand method overriding, using super() to extend functionality, and customizing constructors in subclasses to include additional attributes. This lesson helps you grasp the fundamentals of creating flexible and organized class hierarchies in Python.

Introduction to subclasses

A class inherits all the instance variables and methods of its superclass. (It is a subclass of its superclass.) For example:

class Friend(Person):
  def smile(self):
    print('¯\_(^-^)_/¯')

meg = Friend('Margaret', 25)

In the above example, when you create meg as an instance of Friend, meg will have the say_hi, get_older, and get_much_older methods of the Person class, along with the smile method and the __init__ constructor. Since ...