Guide on Inheritance in Python Classes. Sub-tutorial of the main Advanced handling of classes in Python.
Introduction to Inheritance
Inheritance is the concept of having a class inherit properties from another class. The parent class is the one from which the properties are inherited, and the child class is the one inheriting the properties. The parent class can also be called the base class, and the child class can be called the derived class.
Why use Inheritance?
- Inheritance improves the re-usability of code. Instead of having to write down all that code again, we simply change a one line statement that automatically inherits all functionality from the parent class.
- Inheritance is useful in representing real world relationships as well. Such as that between a Parent and Child, or a Teacher and Student.
The below codes show an inheritance between the class student, and the class human. The parent (base) class here is human and student is the child (derived) class.
class human: def __init__(self, name, gender): self.name = name self.gender = gender def displayname(self): print(self.name) def displaygender(self): print(self.gender) class student(human): def __init__(self, grade, name, gender): self.grade = grade self.name = name self.gender = gender def displaygrade(self): print(self.grade) obj = student(6, "Bob", "Male") obj.displayname() obj.displaygrade() obj.displaygender()
As you can, even though the
obj is an object of the student class, it can call upon the
displaygender methods which are part of the
To some people, this example may seem a little pointless. Why inherit when we could have simply copied pasted those functions into the student class?
See, Classes are not really meant to be used on small scale programs. One of the main benefits of classes is re-usability. Re-usability doesn’t really matter unless your program is at-least moderately sized.
Imagine a large program, where we have dozens of professions, like student , doctor etc. Are we going to replicate the same functions among-st all of them? If there are 5 functions they all have in common, and there are 20 classes, that’s 100 functions created.
The solution is have a common parent class with those 5 functions, and the other 20 classes simply inherit them.
If you’re wondering why would we have 20 different classes for each profession, its because each class is meant to be unique. The student class for instance will have methods and variables dealing with school, graduation, his grade etc. The doctor will have methods and variables dealing with his experience, qualifications etc. It would be a horrible mess if all these classes were combined into one major class.
Due to size restraints, we can’t demonstrate a program of this scale in the examples. But I hope you get the picture.
This marks the end of our Inheritance in Python Classes Guide. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.