Python Class Inheritance

Photo by Scott Webb on Unsplash

Python Class Inheritance

Understanding the use of parent-child relationships in writing reusable code

Inheritance is a programming method that models the relationship that exists between parents and children. A child class inherits variables, methods, and other parameters from an existing class that is a parent class. With the help of the concept of Inheritance, we can create a new class that builds on the features of an existing class.

This is an essential advantage of object-oriented programming as it helps to reduce redundancy by promoting code re-usability.

When a class inherits from an existing class we call it a child class, subclass, or derived class, while the class it inherits from is appropriately called the parent class, superclass, or base class.

Types of Inheritance in Python:

There are four different types of inheritance and this classification is defined by the number of children and parent classes involved and how the parent-child relationship is structured.

Single Inheritance: In single inheritance, there are only one parent and one child classes with the child inheriting directly from just one parent. The child class is a direct extension of the parent class. This type of inheritance is what I also like to call direct inheritance.

class Shape():
    def __init__(self, name, noSides):
        self.name = name
        self.noSides = noSides
    def printInfo(self):
        print(f"A {self.name} has {self.noSides} sides")

class Square(Shape):
    def display(self):
        print("Square child class")

sqr = Square("square", 4)
sqr.printInfo()
sqr.display()
A square has 4 sides
Square child class

Multiple Inheritance: A child class can be declared to inherit from multiple parent classes. When this type of parent-child class relationship exists, it is called a multiple inheritance. Sub-classes created using this method of inheritance have extensive features as compared to both parent classes.

class Parent1():
    def __init__(self):
        // code block
class Parent2():
    def __init__(self):
        // code block

class Child(Parent1, Parent2):
    def __init__(self):
        // code block

Multi-level Inheritance: A multilevel inheritance involves two or more generations of parent classes. A child class A inherits from a parent class B that has also inherited from another parent class C, hence forming a child, parent, and grandparent relationship.

class Grandparent():
    def __init__(self):
        // code block

class Parent(Grandparent):
    def __init__(self):
        // code block

class Child(Parent):
    def __init__(self):
        // code block

Hierarchical Inheritance: In a hierarchical inheritance, one parent class is the base class for multiple child classes.

class Parent():
    def __init__(self):
        // code block

class Child1(Parent):
    def __init__(self):
        // code block

class Child2(Parent):
    def __init__(self):
        // code block

class Child3(Parent):
    def __init__(self):
        // code block

A fifth type of inheritance may be Hybrid inheritance which happens when more than one of the above are combined.

For example,

Parent-Child manipulations:

A child class inherits all variables and methods of its parent class apart from explicitly declared private class variables. This means that a child class is a replica of its parent in the most basic form. But what happens if you want to change some values, or change how a method works in the child class?

Override a parent class method in the child class: When an object or class is declared, it is initialized using the __init__() function declaration. What happens when a derived class is defined without this initialization is that it makes a copy of all of its parent's properties.

So, to override a variable or method in a child class, a variable or method by the same name is declared in the child class' __init__() function declaration.

Conclusion:

This brief introduction to the concept of class inheritance has been able to explain the basics of inheritance in Python programming. Yet this is by no means an exhaustive guide.

For further information, explore the use of the super() function and the isinstance(), type(), and issubclass() methods.