Python Inheritance: Understanding Parent and Child Classes

Learn about inheritance in Python, a key concept in object-oriented programming that allows a class to inherit methods and properties from another class. Discover how to create parent (base) and child (derived) classes, and understand the syntax and benefits of using inheritance to promote code reuse and organization.



Python Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class. The parent class is the class being inherited from, also called the base class. The child class is the class that inherits from another class, also called the derived class.

Create a Parent Class

Any class can be a parent class, so the syntax is the same as creating any other class:

Example

class Person:
    def __init__(self, fname, lname):
        self.firstname = fname
        self.lastname = lname

    def printname(self):
        print(self.firstname, self.lastname)

# Use the Person class to create an object, and then execute the printname method:
x = Person("Nick", "Jones")
x.printname()
        
Output

Nick Jones
        

Create a Child Class

To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:

Example

class Student(Person):
    pass

# Use the Student class to create an object, and then execute the printname method:
x = Student("Nick", "Jones")
x.printname()
        
Output

Nick Jones
        

Add the __init__() Function

We want to add the __init__() function to the child class (instead of the pass keyword). The __init__() function is called automatically every time the class is being used to create a new object.

Note: The __init__() function is called automatically every time the class is being used to create a new object.

Example

class Student(Person):
    def __init__(self, fname, lname):
        Person.__init__(self, fname, lname)
        

When you add the __init__() function, the child class will no longer inherit the parent's __init__() function.

Note: The child's __init__() function overrides the inheritance of the parent's __init__() function.

To keep the inheritance of the parent's __init__() function, add a call to the parent's __init__() function:

Example

class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)
        

Use the super() Function

Python also has a super() function that will make the child class inherit all the methods and properties from its parent:

Example

class Student(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)
        

By using the super() function, you do not have to use the name of the parent element, it will automatically inherit the methods and properties from its parent.

Add Properties

Example

Add a property called graduationyear to the Student class:


class Student(Person):
    def __init__(self, fname, lname):
        super().__init__(fname, lname)
        self.graduationyear = 2022

# Add a year parameter, and pass the correct year when creating objects:
class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

x = Student("Tim", "David", 2022)
        

Add Methods

Example

Add a method called welcome to the Student class:


class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

x = Student("Tim", "David", 2022)
x.welcome()
        
Output

Welcome Tim David to the class of 2022