Inheritance

Inheritance

Inheritance is the concept of creating a new class by reusing an existing class which allows us to get rid of repetitious code and allows us to modify properties from the “parent” (or original) class or add a specific property that will be unique to each “child” (or derived) class. We use the terms parent and child classes, as inheritance transfers properties of the original class to the newly created class.

Let’s look at the syntax of creating a child class using Inheritance:

  • class <child_class_name> (<parent_class_name>):
    ….–code–

Consider an example

class Domestic_Animal:  # Parent Class
    def __init__(self, animal_name):
        self.animal_name = animal_name
        self.animal_sound = ""

    def show_animal_sound(self):
        print(self.animal_name, "makes the sound", self.animal_sound, "\n")


class Livestock(Domestic_Animal):  # Child Class 1
    def __init__(self, animal_name):
        super().__init__(animal_name)

        if self.animal_name == "Cow":
            self.animal_sound = "Mooooo"

        elif self.animal_name == "Pig":
            self.animal_sound = "Oink Oink"


class Pets(Domestic_Animal):  # Child Class 2
    def __init__(self, animal_name):
        super().__init__(animal_name)

        if self.animal_name == "Dog":
            self.animal_sound = "Bow Bow!"

        elif self.animal_name == "Cat":
            self.animal_sound = "Meowww"


animal_1 = Livestock("Cow")
animal_1.show_animal_sound()

animal_2 = Pets("Cat")
animal_2.show_animal_sound()

 

We get this as an output:

Let’s analyze the above code. We first created a class called “Domestic_Animal” and here we defined certain properties, first, we created a variable ‘animal_name’ which stores the name of the animal which is passed as an argument when an object of the class is created, then we initialized an empty variable ‘animal_sound’ which will be utilized by the child class and then finally we used a function to print the name and sound of the animal.

We created two child classes “Livestock” and “Pet” which categorize the types of domestic animals, but for each of these child classes, we have an if statement which stores the precise sound for each animal.

You might be wondering about the following line of code:

super().__init__(animal_name)

When we create a child class it inherits all of the properties of its parent class, their variables, and functions, including the __init__() function. This is why we can use the variables created in the __init__() function of the parent class.

However, in OOP there exists the concept of Function Overriding which means if there exists a function in the child class, with the same name as a function in the parent class, an object will reference the function stored in the child class instead of the parent class.

To illustrate Function Overriding, consider the following example. If you print this you will get the output of the print statement in child class B.

class A:
    def test_function(self):
        print("Function from Parent Class")

class B(A):
    def test_function(self):
        print("Function from Child Class")

obj1 = B()
obj1.test_function()

However, if you change the name of a similar function in class B, you will get the print statement stored in class A.

Likewise in our earlier example, the child class will override the __init__() function from the parent class, and make it empty. So, if we want to retain the __init__() function from the parent class we need to use the super() function inside the __init__() function of the child class. The syntax of using super() is:3

  • def __init__(self, <parameters_of_parent_class>)
    ….super().__init__(<parameters_of_parent_class>)

Now that we have the properties of the parent class, we can now either work with it or build upon it.

Types of Inheritance

The 5 types include:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Simple Inheritance

The first type only involves a single parent class and a single child class. An example:

class A:
    print("Parent")

class B(A):
    print("Child")

Multi-level Inheritance

This type involves two or more child classes that are derived one after another. An example:

class A:
    print("Father")
    print("Grand-father")

class B(A):
    print("Son")
    print("Father")

class C(B):
    print("Son")
    print("Grand-son")

Hierarchical Inheritance

This type forms two or more child classes from a single parent class. An example:

class A:
print("Parents")

class B(A):
print("Child 1")

class C(A):
print("Child 2")

Multiple Inheritance

This type involves the formation of a child class that is derived from 2 parent classes. An example:

class A:
    print("Parents")

class B(A):
    print("Child 1")

class C(A):
    print("Child 2")

Hybrid Inheritance

This type is a combination of hierarchical and multiple inheritances. An example:

class A:
    print("A")

class B(A):
    print("B")

class C(A):
    print("C")
    
class D(B, C):
    print("D")

What have we learned?

  • What is Inheritance?
  • What are parent and child classes?
  • What is Function Overriding?
  • What is the use of the super() function?
  • What are the types of Inheritance?
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments