Inheritance
Creating a new class from existing class is known as inheritance. The class from which features are inherited is known as base class and the class into which features are derived into is called derived class.
Syntax:
- class derived-class(base class):
- <class-suite>
Inheritance promotes reusability of code by reusing already existing classes.
Inheritance is used to implement is-a relationship between classes.
Following hierarchy is an example representing inheritance between classes:
Single inheritance
- class A: #parent class
- #some code
- class b(A): #child class
- #some code
- class father:#parent class
- def father_name(self):
- print("my father name is selvaraj")
- class son(father):#child class
- pass
- obj_father= father()#object for father class.
- obj_son=son()#object for son class.
- obj_son.father_name()# you access son class to father class.
Multilevel inheritance
- class A: #parent class
- #some code
- class B(A): #child class
- #some code
- class C(B): #child class
- #some code
- class g_father:#parent class
- def g_father_name(self):
- print("my father name is ramasamy")
- class father(g_father):#child class
- def father_name(self):
- print("my father name is selvaraj")
- class son(father):#child class
- def son_name(self):
- print("my name is surya")
- obj_g_father= father()#object for g_father class.
- obj_father= father()#object for father class.
- obj_son=son()#object for son class.
- obj_son.g_father_name()# you access son class to g_father class.
- obj_son.father_name()# you access son class to father class.
- obj_son.son_name()# you access same class.
Multiple inheritance
- class A: #parent class
- #some code
- class B(A): #parent class
- #some code
- class C (A, B): #child class
- #some code
- class father:#parent class
- def father_name(self):
- print("my father name is selvaraj")
- class mother:#parent class
- def mother_name(self):
- print("my father name is jayanthi")
- class son(mother,father):#child class
- pass
- obj_father= father()#object for father class.
- obj_mother=mother()#object for mother class.
- obj_son=son()#object for son class.
- obj_son.father_name()# you access son class to father class.
- obj_son.mother_name()# you access son class to mother class.
- #obj_mother.father_name()\\This statement did not work because mother and father are parents.
Hierarchical Inheritance
This inheritance allows a class to host as a parent class for more than one child class or subclass. This provides a benefit of sharing the functioning of methods with multiple child classes, hence avoiding code duplication.
#parent class class Parent: def fun1(self): print(“Hey there, you are in the parent class”) #child class 1 class child1(Parent): def fun2(self): print(“Hey there, you are in the child class 1”) #child class 2 class child2(Parent): def fun3(self): print(“Hey there, you are in the child class 2”) #child class 3 class child3(Parent): def fun4(self): print(“Hey there, you are in the child class 3”) # main program child_obj1 = child3() child_obj2 = child2() child_obj3 = child1() child_obj1.fun1() child_obj1.fun4() child_obj2.fun1() child_obj2.fun3() child_obj3.fun1() child_obj3.fun2() |
In the above code, we have a single parent class and multiple child classes inheriting the same parent class. Now all the child classes can access the methods and variables of the parent class. We’ve created a “Parent” class and 3 child classes “child1”, “child2”, “child3”, which inherits the same parent class “Parent”.
Hybrid Inheritance:
Inheritance consisting of multiple types of inheritance is called hybrid inheritance.
Comments
Post a Comment