image
image
image

Chapter 4: Working with Inheritances

image

Inheritances can be a cool addition to your code that can save a lot of time when writing it out as well. This is something that is prevalent inside of OOP languages because they help you to reuse code and make adjustments to the new version of it, saving a lot of time and making code writing more efficient than before. It also helps to make the code easier to read because you won’t have to rewrite as much stuff in the long run and you can keep everything in a line as you need it.

Basically, an inheritance is one you take one part of your code and then make a second class with it. The second class will have the same information as the first class, but then you can make adjustments and change it up as much as you would like without having any affects on what happens with the first class (no changes will be made to that first class even when you make changes to the second one). You can make a line of inheritances, making a few adjustments as you need depending on what you would like to have happen inside of the code. As you make more inheritances in the code, you will begin to see how much time this can save and how much cleaner and nicer your code will look.

So let’s take a look at taking the base class (or the first class) and creating a new derived class (or the second class) from it when working inside of inheritances:

––––––––

image

#Example of inheritance

#base class

class Student(object):

def__init__(self, name, rollno):

self.name=name

self.rollno=rollno

#Graduate class inherits or derived from Student class

class GraduateStudent(Student):

def__init__(self, name, rollno, graduate):

Student__init__(self, name, rollno)

self.graduate=graduate

def DisplayGraduateStudent(self):

print”Student Name:”, self.name)

print(“Student Rollno:”, self.rollno)

print(“Study Group:”, self.graduate)

#Post Graduate class inherits from Student class

class PostGraduate(Student):

def__init__(self, name, rollno, postgrad):

Student__init__(self, name, rollno)

self.postgrad=postgrad

def DisplayPostGraduateStudent(self):

print(“Student Name:”, self.name)

print(“Student Rollno:”, self.rollno)

print(“Study Group:”, self.postgrad)

#instantiate from Graduate and PostGraduate classes

objGradStudent = GraduateStudent(“Mainu”, 1, “MS-Mathematics”)

objPostGradStudent = PostGraduate(“Shainu”, 2, “MS-CS”)

objPostGradStudent.DisplayPostGraduateStudent()

When you type this into your interpreter, you are going to get the results:

(‘Student Name:’, ‘Mainu’)

(‘Student Rollno:’, 1)

(‘Student Group:’, ‘MSC-Mathematics’)

(‘Student Name:’, ‘Shainu’)

(‘Student Rollno:’, 2)

(‘Student Group:’, ‘MSC-CS’)

Why would I override the base class?

There are some times while working on your code when it is important to override your base class to make it into a brand new derived class. This means that you are going to look at the base class and replace the behavior of it so that it is now present inside of the child class that you are trying to create.

The nice thing about this is that you can take those parental features that you like, the ones that you would like to keep, and use them in the derived class. You can make any changes that you would like in the child class while still maintaining the parts of the original class that you want to keep around. When you use the override method, you will be able to copy over your other class without having issues with duplicating any of a code that could cause issues, and it still allows you to get the parts that you want while enhancing and changing them to get the type of new code that you would like.

Overloading

Another thing that you can learn to do when working in inheritance is a process called overloading. When you go through and do overloading, you are taking one identifier and using it to define more than one method; usually this will be just two methods inside the class, but it can be more. The two methods need to be in the same class, but they would have different parameters that are associated with it. This method is going to be used the most often when you want the methods to execute the same kind of task, but you would like to have them work with different parameters.

As a beginner, there probably aren’t too many times when you would want to use overloading (and it isn’t that common even later on), but it is still a good idea to understand what it is about in case you see it in some other codes that you are working on. If you do need to use the process of overloading, you should download the extra module that will help you to get this work done.

Working with multiple inheritance

Another feature that you are able to work with when on Python is a thing that is known as multiple inheritance. This is similar to a normal inheritance, but we are going to take it a step further. With this type of inheritance, you will be able to take a class and give it two or more parent classes. This can really help you to grow your code without having a mess of codes all over the place and having to repeat yourself inside the code all the time.

What will happen when you are working on multiple inheritance, you are creating a new class, Class3, which was derived from the features that you had in Class2, and then Class2 was created from the features of Class1. Class3 is going to have some features that came from Class2, but it should also have some features from Class1 in it as well. Each layer will have some of the features of the one before it, but you are able to make some changes to help the code work in a new way as you like inside Python.

While you are able to keep moving the code on with multiple inheritance, you should remember that you aren’t able to work with a circular inheritance. But you can use as many parent classes as you would like. You can expand the example above to a Class4, which would get features from all the classes above it and then you can use Class4 in order to create a Class5 as well. This can go on for as long as you would like, you just need to make sure that you copy over the codes properly and that you make the changes that you want before moving on to the next leg of the inheritance.

The idea of multiple inheritance is popular inside of your Python program. There are many times when you would like to use the same block of code inside the program you are writing, but maybe you would like to change the result or the function or something else that comes out each time that the program goes through that part of the code. Inheritances can help you to rewrite the code a few times over in an efficient manner that doesn’t take up too much space within the code. Learning how to make this work for your needs can save a lot of time and hassle and makes your code look better.