Inheritance in Python
One of the unique selling points of Python, why Python is so famous, because Python supports all types of programming paradigms. It supports:
- Functional Programming
- Object Oriented Programming
- Procedure Oriented Programming
Let’s brush up on some important points that we use in Object Oriented Programming in Python.
Class: Class is a design or you can call blueprint.
Object: Object is an instance of a class. It has attributes and behavior.
__init__ method: It is a special method in python which we use to initialize the variables. It will be getting called automatically for every object.
Variable: It has two types:
- Instance variables: Declared inside the __init__ method. It can be changed depending on the Objects.
- Class/static variables: Declared inside class but outside __init__ method. It will be the same for every Object.
Methods: It is used to perform operations. It has three types:
- Instance method
- Class method
- Static method
Example # Class declaration class Mobile: mobile = ‘Nokia’ def __init__(self, ram, memory): self.ram = ram self.memory = memory def config(self): print(“Configuration is: ”, self.ram, self.memory) @classmethod def getMobileName(cls): print(“Mobile is: “, cls.mobile) @staticmethod def information(): print(“This gives the mobile information”) # Object creation mob = Mobile(“4gb”, “64gb”) # Calling method config Mobile.config(mob) OR mob.config() # Here by default mob is passed as a parameter # Calling class method Mobile.getMobileName() # Calling static method Mobile.information()
It is the ability of one class to inherit or derive the properties of another class. The class which inherits another class is called subclass or child class and the class from which it inherits is called super class or parent class. Sub class can access all the features of super class but vice versa is not possible.
Why do we use Inheritance:
- It represents real-world relationships really well.
- It provides reusability of code which reduces the length of code. We don’t have to write the same code time and again. Also, it allows us to add more features to a class without modifying it.
- It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
Type of Inheritance:
There are three types of inheritance possible in python. For example let’s say we have three classes A, B and C.
- Single Level Inheritance: When class B extends class A only.
- Multi Level Inheritance: When class B extends class A and class C extends class B.
- Multiple Inheritance: When class C extends both class A and B.
Constructor(__init__) in Inheritance:
If we create an object of a subclass, it will first try to find init of subclass. If it is not found, it will call the init of super class. In case we want to call the init method of both the classes from the object of subclass then we use a method called super(). For better understanding please check the below-given example.
Method Resolution Order (MRO):
Whenever we look for an attribute in a class which is involved in multiple inheritances, an order is followed. First, it is searched in the current class. If not found, the search moves to parent classes in left-to-right order.
Example class A: def __init__(self): print(“ Inside A init ”) def feature1(): print(“ feature 1 in A is working”) def feature2(): print(“ feature 2 is working”) class B: def __init__(self): print(“ Inside B init ”) def feature1(): print(“ feature 1 in B is working”) class C(A,B): def __init__(self): super().__init__() print(“ Inside C init ”) c = C() c.feature1() Output Inside A init # we called super() inside init of C. Inside C init # init of C will be called. feature 1 in A is working # from MRO rule class A will be the super class.