Table of Contents
Introduction To Inheritance In Python
Python programming is also object-oriented programming. Inheritance in python is a powerful feature in object-oriented programming.
What is meant by Inheritance?
In object-oriented programming, inheritance is the concept that when a class of objects is defined, any subclass. (i.e. Child class is joined to parent class).simply, Inheritance is when a class uses code constructed within another class. In terms of biology, we can think of a child inheriting certain traits from their parents.
Parent Class
It is the class being inherited from, also called a base class.
Child Class
It is the class that inherits from another class, also called a derived class.
Benefits of Inheritance:
- Code re usability, here are many possible methods to let you reuse the same functions and properties in your code.
- It represents real-world relationships well (i.e. in-between parent class and child class).
- It is transitive in nature. If a child class inherits properties from a parent class, then all other sub classes of the child class will also inherit the properties of the parent class.
This tutorial will go through some of the major aspects of inheritance in Python, including how parent classes and child classes work.
Create a Parent Class
Parent class is also called base classes. Parent classes create a pattern out of which child or sub classes can be based on. It can have the ability to create a child class through inheritance without having to write the same code over again each time.
For example: Creating a bank account, now a ‘ bank account ’ as a parent account that has a ‘ personal account ’ and ‘ business account’ child classes. Many of the methods between personal and business accounts will be similar, such as methods to withdraw and deposit money, so those can belong to the parent class of ‘ bank_account ’.
Let’s create a ‘Fish’ parent class that we
Create a Child Class
Children are classes that will inherit from the parent class. In simple words, it means that each child’s class will be able to make use of the methods and variables of the parent class.
For example, business_account is a child class of bank_account.
class account(Bank_Account):
pass
Note: ‘ pass ’ keyword when you do not want to add any other properties or methods to the class.
Created a child class that inherits the properties and methods from its parent.
Using __int__ ( ) function to the child class(instead of the ‘pass’ keyword).
Using super() function that will make the child class inherit all the methods and properties from its parent.
sub_classing
The parent class by mentioning the parent class name in the declaration of the child class is called ‘ sub-classing ’.
__int__() function is called automatically every time the class is being used to create a new project. The child class __int__() function overrides the parent class’s __int__() function.
Types of Inheritance In Python
Depending upon the number of classes(i.e. Child and parent classes). There are four types of inheritance in Python.
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
Single Inheritance
Only one child class inherit only one parent class.
Example:
class Parent:
pass
class Child(Parent):
Pass
class Parent:
def funcl(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function two")
y = Child()
y.funcl()
y.func2()
Output:
this is function one
this is function two
Multiple Inheritance
A class can be derived from more than one base class in Python. This is called multiple inheritances. In other words, child class inherits from more than one parent class.
Example:
class Parent1:
pass
class Parent2:
pass
class MultiDerived(Parent1, Parent2):
pass
class Parentl:
def functionl(self):
print("this is first function 1")
class Parent2:
def functionl(self):
print("this is second function 2")
class Parent3:
def function3(self):
print("this is third function 3")
class Child(Parentl, Parent2, Parent3):
def function4(self):
print("this is fourth function 4")
z = Child()
z.functionl()
z.function2()
z.function3()
z.function4()
Output:
this is first function 1
this is second function 2
this is third function 3
this is fourth function 4
Widget not in any sidebars
Multilevel Inheritance
We can also inherit from a derived class. This is called multilevel inheritance.
In multilevel inheritance, features of the base(parent) class, and the derived class are inherited into the new derived class.
Example:
class Parent:
pass
class Derived1(Parent):
pass
class Derived2(Derived1):
Pass
class Family:
def show family(self):
print("This is our family:")
# Father class inherited from Family
class Father(Family):
fathername = ""
def show father(self):
print(self.fathername)
# Mother class inherited from Family
class Mother(Family):
mothername = ""
def show mother(self):
print(self.mothername)
# Son class inherited from Father and Mother classes
class Son(Father, Mother):
def show parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
sl = Son() # Object of Son class
sl.fathername = "Mark'
s1.mothername = 'Sonia"
sl.show_family()
sl.show_parent
Output:
This is our family:
('Father :', 'Mark')
('Mother :', 'Sonia')
Hierarchical Inheritance
When more than one derived class is created from a single base.
class Parent:
def functionl(self):
print("this is first function 1")
class Childl(Parent):
def function2(self):
print("this is second function 2")
class Child2(Parent):
def function3(self):
print("this is third function 3")
x = Childl()
xl = Child2()
x.functionl()
x.function2()
Output:
this is first function 1
this is second function 2
Super function
Super function allows us to call a method from the parent class.
The super() builtin returns a proxy object (temporary object of the superclass) that allows us to access methods of the base class.
Major Two Advantage
- Working with Multiple Inheritance
- Allows us to avoid using the base class name explicitly
class Mammal(object):
def __init__ (self, mammalName):
print(mammalName, 'is a warm-blooded animal.')
class Dog(Mammal):
def __init__ (self):
print('Dog has four legs.')
super(). __int__ ('Dog')
dl = Dog()
Output:
Dog has four legs
Widget not in any sidebars
Conclusion:
You are learning about the class and the types of classes. Also learn the inheritance relationships and composition creating. This article, you learned, uses of inheritance, evaluate if the inheritance is the right relationship, multiple inheritances in python. An important thing is covered, reuse existing code through policy design based on composition.