Inheritance is a unique feature of any object-oriented programming language. It is a simple concept. Consider this: your father owns a car, which means (according to Bangladeshi customs) it is also your car, even though you might own another car yourself. Here, your father’s car is inherited by you.

The same applies to programming. When an attribute or method of a parent class is inherited by a child class, it is called inheritance.

In the previous section, we discussed only a single class. In this section, we will look at an example of inheritance using multiple classes.

# This is an example of inheritance in Python
class MathClass: # Parent Class
    def __init__(self,x,y):
    def multiply_function(self):
        return z
class MathClass2(MathClass): # Child Class
    def __init__(self,x,y):
    def division_function(self):
        return z
firstObject=MathClass(5,10) # creating an object
secondObject=MathClass2(4,2) # creating an object
print(firstObject.Multiply_function()) # Accessing a method
print(secondObject.Division_function()) # Accessing a method
print(secondObject.Multiply_function()) # Accessing a method using inheritance



Here, we have two classes, MathClass and MathClass2. In line 23, the multiply_function() is called on secondObject; however, there is no function by that name in MathClass2. This function is actually inherited from MathClass.

Previously, we used the self keyword, but for inheritance, we need to specify the class we want to inherit within the class definition, as shown in line 10.

Parent Class

The class that is inherited is called the parent class. In the example above, MathClass used in line 2 is the parent class or base class.

Child Class

The class that inherits from another class is called the child class. In the example above, MathClass2 in line 10 is a child class.

0 0 votes
Article Rating
Would love your thoughts, please comment.x