Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
Python programming language is easy to learn and works on both procedural and object oriented programming approach. Inheritance is one such concept in object oriented programming. Code reusability being the forte of inheritance, it helps in a lot of applications when we are working on Python. Following are the concepts discussed in this article:
To master your skills and kick-start your learning enroll to Edureka’s python certification program and become a python developer in no time.
The method of inheriting the properties of parent class into a child class is known as inheritance. It is an OOP concept. Following are the benefits of inheritance.
Code reusability- we do not have to write the same code again and again, we can just inherit the properties we need in a child class.
It represents a real world relationship 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.
Below is a simple example of inheritance in python.
class Parent(): def first(self): print('first function') class Child(Parent): def second(self): print('second function') ob = Child() ob.first() ob.second()
Output: first function second function
In the above program, you can access the parent class function using the child class object.
Calling a constructor of the parent class by mentioning the parent class name in the declaration of the child class is known as sub-classing. A child class identifies its parent class by sub-classing.
The __init__() function is called every time a class is being used to make an object. When we add the __init__() function in a parent class, the child class will no longer be able to inherit the parent class’s __init__() function. The child’s class __init__() function overrides the parent class’s __init__() function.
class Parent: def __init__(self , fname, fage): self.firstname = fname self.age = fage def view(self): print(self.firstname , self.age) class Child(Parent): def __init__(self , fname , fage): Parent.__init__(self, fname, fage) self.lastname = "edureka" def view(self): print("course name" , self.firstname ,"first came", self.age , " years ago." , self.lastname, " has courses to master python") ob = Child("Python" , '28') ob.view()
Depending upon the number of child and parent classes involved, there are four types of inheritance in python.
When a child class inherits only a single parent class.
class Parent: def func1(self): print("this is function one") class Child(Parent): def func2(self): print(" this is function 2 ") ob = Child() ob.func1() ob.func2()
When a child class inherits from more than one parent class.
class Parent: def func1(self): print("this is function 1") class Parent2: def func2(self): print("this is function 2") class Child(Parent , Parent2): def func3(self): print("this is function 3") ob = Child() ob.func1() ob.func2() ob.func3()
When a child class becomes a parent class for another child class.
class Parent: def func1(self): print("this is function 1") class Child(Parent): def func2(self): print("this is function 2") class Child2(Child): def func3("this is function 3") ob = Child2() ob.func1() ob.func2() ob.func3()
Hierarchical inheritance involves multiple inheritance from the same base or parent class.
class Parent: def func1(self): print("this is function 1") class Child(Parent): def func2(self): print("this is function 2") class Child2(Parent): def func3(self): print("this is function 3") ob = Child() ob1 = Child2() ob.func1() ob.func2()
Hybrid inheritance involves multiple inheritance taking place in a single program.
class Parent: def func1(self): print("this is function one") class Child(Parent): def func2(self): print("this is function 2") class Child1(Parent): def func3(self): print(" this is function 3"): class Child3(Parent , Child1): def func4(self): print(" this is function 4") ob = Child3() ob.func1()
Super function allows us to call a method from the parent class.
class Parent: def func1(self): print("this is function 1") class Child(Parent): def func2(self): Super().func1() print("this is function 2") ob = Child() ob.func2()
Method Overriding
You can override a method in python. Look at the example below.
class Parent: def func1(self): print("this is parent function") class Child(Parent): def func1(self): print("this is child function") ob = Child() ob.func1()
The functionality of the parent class method is changes by overriding the same method in the child class.
Inheritance is one of the most important concept of OOP. It provides code reusability, readability and transition of properties which helps in optimized and efficient code building. Python programming language is loaded with concepts like inheritance. Enormous python applications calls for increased number of python programmers in the recent market. To master your skills and kick-start your learning enroll to Edureka’s python certification program and become a python developer in no time.
Have any questions? mention them in the comments section. Our team will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 25th January,2025 25th January SAT&SUN (Weekend Batch) | View Details |
edureka.co