26 Apr How to Use Inheritance in Python
Inheritance is referred to as the act of inheriting something or when it’s passed down to someone else. In this tutorial, we’ll be looking at what inheritance is in Python. That is, how we can use it and how it’s being created. Inheritance in Python is the ability of a class to acquire or inherit properties from another class. The class it inherits is called base (or parent class) and the new class is called derived (or child) class.
The capability to inherit properties from another class helps to promote the reusability of code, whereby we don’t repeat code we’ve written earlier. Inheritance is also known as a powerful feature in object-oriented programming.
We’ll demonstrate different examples of how we can use inheritance in Python.
Single Inheritance
In Single inheritance, this is how a class can inherit from only a single parent class of which it has access to properties of the base class.
In the example below, we will demonstrate a basic example of how (we) a Student class is inheriting from the base class(Persons), which gave it access to elements in the base class.
1class Person: 2 def __init__(self, firstName, lastName): 3 self.fname=firstName 4 self.lname=lastName 5 6 def printname(self): 7 print(self.fname, self.lname) 8 9 10class Student(Person): 11 pass 12user = Student("Mike", "Johnson") 13user.printname() 14#Result 15Mike Johnson
Multiple Inheritance
Multiple Inheritance refers to how a class inherits from more than one base class and all features of the base classes are inherited into the derived class. This type of inheritance is called multiple inheritance.
In this example, we demonstrated with a bio-data information whereby we have 2 different base classes (Name, Age), which the Bdata inherited from and it also gain access to functions of both classes.
1# Base class1 2class Name: 3 PName = "" 4 def Name(self): 5 print(self.PName) 6 7# Base class2 8class Age: 9 Agevalue = "" 10 def Age(self): 11 print(self.Agevalue) 12 13# Derived class 14class BData(Name, Age): 15 def userData(self): 16 print("Name :", self.PName) 17 print("Age :", self.Agevalue) 18 19the_data = BData() 20the_data.PName = "Grace" 21the_data.Agevalue = "15" 22the_data.userData() 23 1#Result 2Name : Grace 3Age : 15
We have another example below, where we have the Base class
‘Intern’, ‘FullWorker’
and also a child class
TeamLeader
which inherits the 2 base class (
‘Intern’, ‘FullWorker’
) and gets to access all properties and elements of each of those classes.
2# Parent class 1 3class Intern(object): 4 def __init__(self, name): 5 self.name = name 6 7# Parent class 2 8class FullWorker(object): 9 def __init__(self, salary, jobtitle): 10 self.jobtitle = jobtitle 11 self.salary = salary 12 13# Deriving a child class from the two parent classes 14class TeamLeader(Intern, FullWorker): 15 def __init__(self, name, jobtitle, salary, experience): 16 self.experience = experience 17 Intern.__init__(self, name) 18 FullWorker.__init__(self, salary, jobtitle) 19 print("Name: {}, Pay: {}, Exp: {}".format(self.name, self.salary, self.experience)) 20 21the_teamlead = TeamLeader('Bill','Data Engineer', 300000, 4) 1#Result 2Name: Bill, Pay: 300000, Exp: 4
Thanks for reading this article. Feel free to drop your comments and questions in the comments section below.
No Comments