Python Inheritance
By: Daniel Liu
Tags: Programming, Python Programming, Object Oriented, Oop, Python

Overview

Inheritance is one of the four pillars of Object Oriented Programming(OOP). Inheritance allows classes to inherit all methods and properties from another class. The parent class is the class that is being inherited from also known as the base class. The child class is the class that is inheriting from another class.

Photo by Markus Spiske on Unsplash

Parent Class

The parent class is a typical class that just needs to have the methods and properties that will be inherited.

class Parent:

def __init__(self, name):

self.name = name



def printName(self):

print(self.name)

Child Class

The child class can be a typical class but you just need to wrap the parent class in parenthesis right after the child class.

class Child(Parent):

pass x = Child("Mike")

x.printName() >> Mike

The pass keyword is used when the class does not have any method or properties.

__init__()

The __init__() function is called every time a class is created. When a child class inherits from the parent class the parents class __init__() function is overwritten by the child’s class __init__() function. This means that only the child’s __init__() function will be called not the parents or both.

class Parent:

def __init__(self, name):

print("Parent")

self.name = name



def printName(self):

print(self.name) class Child(Parent):

def __init__(self, name):

print("Child")

self.name = name x = Child("Mike")

x.printName() >> Child

>> Mike

Super()

The super() function references the parent class that was inherited from which gives you access to the parents properties and methods.

class Parent:

def __init__(self, name):

print("Parent")

self.name = name



def printName(self):

print(self.name) class Child(Parent):

def __init__(self, name):

print("Child")

self.name = name x = Child("Mike")

x.printName() >> Parent

>> Mike

New Properties

When the child class inherits from the parent class you can add properties to the child class to which the parent class does not have.

class Parent:

def __init__(self, name):

print("Parent")

self.name = name def printName(self):

print(self.name) class Child(Parent):

def __init__(self, name, age):

print("Child")

self.name = name

self.age = age

def getAge(self):

print(self.age) x = Child("Mike", 9)

x.printName()

print(x.age) >> Parent

>> Mike

>> 9

New Methods

The child class can have its own methods while inheriting from the parent class.

class Parent:

def __init__(self, name):

print("Parent")

self.name = name

def printName(self):

print(self.name)



class Child(Parent):

def __init__(self, name, age):

print("Child")

self.name = name

self.age = age

def getAge(self):

print(self.age)



x = Child("Mike", 9)

x.printName()

x.getAge() >> Child

>> Mike

>> 9

If the child class has the same method name as the parent class it will overwrite the parent class method.

class Parent:

def __init__(self, name):

print("Parent")

self.name = name

def printName(self):

print(self.name)



class Child(Parent):

def __init__(self, name, age):

print("Child")

self.name = name

self.age = age

def getAge(self):

print(self.age)

def printName(self):

print("Child " + self.name)



x = Child("Mike", 9)

x.printName()

x.getAge() >> Child

>> Child Mike

>> 9

To call the parent class method that was overwritten you would have to use the super() method.

class Parent:

def __init__(self, name):

print("Parent")

self.name = name

def printName(self):

print("Parent " + self.name)



class Child(Parent):

def __init__(self, name, age):

print("Child")

self.name = name

self.age = age

def getAge(self):

print(self.age)

def printName(self):

super().printName()



x = Child("Mike", 9)

x.printName()

x.getAge() >> Child

>> Parent Mike

>> 9

Conclusion

Inheritance helps represent real world relationships. It helps with reusability by reducing code. If multiple classes will use the same properties and methods you can have a single parent class and multiple child class that inherit from the parent. Inheritance is also transitive so if a class inherits from a child class that inherits from the parent class then the class gets both the methods and properties from the child class and parent class.