Introduction:-
One of the major advantages of Object Oriented Programming is re-use. Inheritance is one of the mechanisms to achieve it.
In inheritance, a class (usually called superclass/parent class) is inherited by another class (usually called subclass/child class/derived class). The subclass adds some attributes to superclass.
In inheritance, the child class acquires the properties and can access all the data members and functions defined in the parent class.
Python Inheritance Syntax-
class BaseClass: Body of base class class DerivedClass(BaseClass): Body of derived class
- Parent class is the class being inherited from, also called Base class.
- Child class is the class that inherits from another class, also called Derived class.
The benefits of inheritance are: –
- Real-world relationships :- It represents real-world relationships well.
- Code re-usability:- It provides re-usability of a code because we can use an existing class to create a new class instead of creating it from scratch. So we don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
- Transitive:-It is transitive in nature, which means that if class Y inherits from another class X, then all the sub-classes of Y would automatically inherit from class X.
Let’s understand it using one example below-
Let’s understand it using an example below-
Let’s create a class Polygon with attributes called width & Height. Let’s create another class called “Rectangle”. And since we know that every Rectangle is a Polygon so we can inherit Polygon class in Rectangle class and we won’t have to write code from scratch in Rectangle class. We can just inherit Base class i.e. Polygon properties in child class i.e Rectangle by passing Polygon class inside Rectangle class. And later we can add whatever additional methods we want to add in Child class i.e. Rectangle class. And it save our development time & makes our code more reusable.
class Polygon: __width = None __height = None def set_values(self, width, height): self.__width = width self.__height = height def get_width(self): return self.__width def get_height(self): return self.__height class Rectangle(Polygon): def area(self): return self.get_width() * self.get_height() class Triangle(Polygon): def area(self): return self.get_width() * self.get_height() / 2 rect = Rectangle() tri = Triangle() rect.set_values(50, 40) tri.set_values(50, 40) print(rect.area()) print(tri.area())
Multiple inheritance: –
Ability of a class to inherit from multiple classes is called multiple inheritance.
When you want to inherit from super class to subclass then there’s “is/are” relationship between superclass and subclass.
class derive-class(<base class 1>, <base class 2>, ..... <base class n>): <class - suite>
Let’s understand it using an example below-
>>> class Parent1: def __init__(self, name): print('Parent1 __init__', name) >>> class Parent2: def __init__(self, name): print('Parent2 __init__', name) >>> class Child1(Parent2, Parent1): def __init__(self): print('Child __init__') Parent2.__init__(self, 'max') Parent1.__init__(self, 'tom') >>> print('-------------------Example 1--------------------') >>> child = Child1()
The output of the above code looks like below-
Output:- -------------------Example 1-------------------- Child __init__ Parent2 __init__ max Parent1 __init__ tom
As we can see that we’ve inherited we’ve inherited properties of both Parent1 & Parent2 class in Child1 class, in the order they’ve been passed, successfully.
Hey, but do you know you inherit the Base class in Child class using super() function in Python like shown below. And this is the topic for our next post which talks about super() function in Python. So stay tuned!!
>>> class Child2(Parent1): def __init__(self): print('Child __init__') super().__init__('max') >>> print('-------------------Example 2--------------------') >>> child2 = Child2() output:- -------------------Example 2-------------------- child __init__ Parent1 __init__ max