Skip to main content

Python Inheritance

With inheritance, one class can take on all the methods and properties of another class.

The class that gives them away is the parent class, sometimes called the base class.

The class that receives them is the child class, also known as the derived class.


Create a Parent Class

A parent class needs no special syntax. You write it like any other class, and any class can take this role:

Example

Person stores firstname and lastname properties and offers a printname method:

Prompt
Write a Python class Person with firstname and lastname properties and a printname method. Create a Person object and call printname.
Output: complete Python program

Create a Child Class

To make a child class, write the parent's name in parentheses after the new class name, so the functionality carries over:

Note: pass is what you write when the class needs nothing of its own beyond what it inherits.

As a result, Student ends up with every property and method that Person has.

Example

Student takes its properties and methods from the Person class:

Prompt
Write a Python class Student that inherits from Person using pass. Create a Student object and call the inherited printname method.
Output: complete Python program
main.py
class Student(Person):
  pass

Add the __init__() Function

Our child class so far simply receives everything from its parent.

Next, replace the pass keyword with an __init__() function of its own.

Note: Python calls __init__() by itself whenever the class creates a new object.

There is a catch: a child class with its own __init__() stops inheriting the parent's version.

Note: The child's __init__() overrides the one it would have inherited from the parent.

To get the parent's behavior back, call the parent's __init__() from inside the child's:

The child now has its own __init__() and still keeps what the parent provides, so it is ready for you to add new functionality.

Example

Prompt
Write a Python child class Student that calls the parent Person init using Person.init().
Output: complete Python program
main.py
class Student(Person):
  def __init__(self, fname, lname):
    #add properties etc.

Use the super() Function

The built-in super() function is another way to reach the parent from a child class:

Because super() finds the parent for you, the parent's name never has to appear in the call, and you still get its methods and properties.

Example

Prompt
Write a Python child class Student that uses super().init() to inherit from Person.
Output: complete Python program

Add Properties

A graduation year such as 2019 should not be written into the class. It should be a variable, given to Student each time a student object is created, and that takes one more __init__() parameter:

Example

Add a year parameter, and supply the right value as each object is created:

Prompt
Write a Python Student class inheriting from Person, adding a graduationyear property. Create a Student with year 2019.
Output: complete Python program

Add Methods

Child classes can have methods of their own. One thing to watch: a child method that shares its name with a parent function overrides the inherited one.

Example

Give the Student class a welcome method:

Prompt
Write a Python Student class inheriting from Person with a welcome method that prints a greeting with graduationyear.
Output: complete Python program