In this tutorial, we’ll learn how to use Python inheritance. We’ll be uncovering the main concepts of inheritance in Python. We’ll see what its role is and how to properly use it.
Outline
- Introduction: Python Inheritance
- Syntax
- Implementing Python Inheritance (Multiple Examples)
- Example 1: Calling Method of Base Class Using Object of Child Class
- Example 2: Simple Addition Using Python Inheritance
- Example 3: Accessing Attributes and Method of Parent Class in Child Class (2 Methods)
- Method 1: Using Parent Class Name
- Method 2: Using super()
- Example 4: Method Overriding
- Conclusion
Introduction: Python Inheritance
This concept is very beneficial in Python. By using this , we can reuse the existing code. Its superb to organize the code.
For instance, we’ve two classes. Class 1 and class 2. Class 2 is inheriting all the attributes and functions of class 1, so class 2 is a derived/child class. While class 1 is a parent/base class.
Let’s now understand inheritance in Python using practical code examples.
Syntax
Let’s first understand the syntax of how inheritance is implemented between Python classes.
class BaseClass: class DerivedClass(BaseClass):
- Using parenthesis after child class name and specifying the name of parent class will link these two classes as parent and child.
- We can now easily reuse the attributes and methods of parent/base class using the new derived class.
Implementing Python Inheritance (Multiple Examples)
Below examples will practically demonstrate inheritance in Python in detail.
Example 1: Calling Method of Base Class Using Object of Child Class
class ClassA: def parentFun(self): print('Hye, How\'s Life') class ClassB(ClassA): def childFun(self): print('Good') obj=ClassB() obj.childFun() obj.parentFun()
We can see in the above code that object of class B has called the function of class A as well. It means attributes or methods in class A are now accessible by class B. This is how Python inheritance works.
Example 2: Simple Addition Using Python Inheritance
class ClassA: a=40 b=50 class ClassB(ClassA): c=45 obj=ClassB() print(obj.c+obj.a+obj.b)
This is how we can easily access the attributes of parent class using the object of derived class.
Example 3: Accessing Attributes and Method of Parent Class in Child Class (2 Methods)
We can use two method through which we can easily access the attributes of functions of parent/base class in child/derived class. Let’s understand them one by one.
Method 1: Using Parent Class Name
class ClassA: a=5 b=15 class ClassB(ClassA): a=10 val1= ClassA.a val2= ClassA.b def addFun(self): print(self.a+self.val1+self.val2) obj=ClassB() obj.addFun()
We can see that by using the name of parent/base class, we’ve successfully fetched the values of parent class in child class.
We can access the methods of parent class from within child class as well. See below code:
ParentClassName.parentMethod(self)
Method 2: Using super()
class ClassA: aa=2 bb=5 class ClassB(ClassA): a=20 def addFun(self): val1= super().aa val2= super().bb print(self.a+val1+val2) obj=ClassB() obj.addFun()
We can use super() to make use of the attributes of parent class in child class.
We can also call methods of parent class using these two methods. See below code:
class ClassA: aa=2 bb=5 def add(self): cc=0 cc=self.aa+self.bb return cc class ClassB(ClassA): a=20 def fun(self): b= ClassA.add(self) // method 1: through class name print(self.a+b) print(super().add()) // method 2: through super() obj=ClassB() obj.fun()
Above code shows that methods of parent class can also be accessed through class name or super(). Just make sure that when using the class name, use self with it (className.fun(self)). But if you are using super(), then no need to use self (super().fun()).
Example 4: Method Overriding
class A: def fun(self): print('This is Class A') class B: def fun(self): print('This is Class B') obj=B() obj.fun()
Output
This is Class B
We’ve method with same name in both the base and derived class. We’ve created an object of derived class and tried to call the method of name (fun). It first checked that method in its own class. In our case, the method is found in the same class so it gets executed. If it was not found, then it’d have checked it in the base class. See below example:
class AA: def fun(self): print('This is Class A') class BB(AA): def funS(self): print('This is Class B') obj=BB() obj.fun()
Output
This is Class A
We can see that as the method was not found in the same class so it checked for it in its parent class as shown in the above output.
So this is how we can easily use Python inheritance. Hope you like this post and if you do, then do share it with your developer friends.
Feel free to ask if you have any questions related to the implementation of Python inheritance. We’d be very delighted to answer all of them.
Click here if you want to learn more about it.
Conclusion
To conclude this tutorial, hope you now have an in-depth practical understanding of Python inheritance. We’ll be very happy to receive your feedback on this article.
Do visit my other articles on Python programming. Thank you for reading this post.