Subclasses

You'll learn about the concept of subclasses in Python in this lesson.

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 the constructor is inherited, you can use the same arguments to create a Friend that you can use to create a Person, as you can see in both the examples above and below:

meg = Friend('Margaret', 19)

Check out the example below:

Get hands-on with 1200+ tech skills courses.