Inheritance and its Types🤩
INHERITANCE
- Example- Cars have same property -> brake( ), acceleration( ), gear( ). So, if we make make a Car class, we can create object for different cars and use this properties again and again.
- In, python, it is also know as Is-A relationship.
class Student:
def__init__(self,name,rollno,marks):
self.name = name
self.rollno = rollno
self.marks = marks
def Info(self):
print('Student name is ',self.name,'roll no ',self.rollno)class Result(Student):
def res(self):
if(self.marks>50):
print("pass")
else:
print("Fail")In the above program Student class is the parent class and the Result class is the subclass. We can perform the methods defined in the Student class also through the Result class.
t = Result('tom',4067,70)
t.Info()t.res()>> Student name is tom rollno is 4067 pass
COMPOSITION
Composition is a concept that models a has a relationship. It enables creating complex types by combining objects of other types. This means that a class can contain an object of another class. This relationship means that a Class has a Class.
Example:
class Student:
def__init__(self,name,rollno,marks):
self.name = name
self.rollno = rollno
self.marks = marks
def Info(self):
print('Student name is ' ,self.name, 'roll no is ' , self.rollno)class Result:
def __init__(self,name,rollno,marks)
self.student = Student(name,rollno,marks)
def printInfo:
self.student.Info()s = Result('sam',4067,70)
s.printInfo()>> Student name is sam roll no is 4067The composition is similar to inheritance but we cannot call the methods of the class that we defined inside the result class.
Inheritance vs Composition
Inheritance is defined as ‘is a relationship’ where Composition is defined as ‘has a relationship’. In Inheritance, a class is derived as a subclass of another class wherein composition a class is somewhat called inside another class.Let us see an example by performing the same operations using inheritance and composition.
Child class
We can see that we can directly call a method of a parent class in subclass using Inheritance whereas in composition we have to define it under the other module.Benefits of using Inheritance
Here are a few main advantages of using Inheritance in your programs.
- Less code repetition, as the code which is common can be placed in the parent class, hence making it available to all the child classes.
- Structured Code: By dividing the code into classes, we can structure our software better by dividing functionality into classes.
- Make the code more scalable.
TYPES OF INHERITANCECreating a Child Class
In Python, we use the following general structure to create a child class from a parent class.
SYNTAX
class ChildClassName(ParentClassName): ChildClass implementation
Simple Inheritance (or) Single Inheritance
In this type of inheritance, one child class derives from one parent class. Look at the following example code.
Example
class ParentClass: def feature_1(self): print('feature_1 from ParentClass is running...') def feature_2(self): print('feature_2 from ParentClass is running...') class ChildClass(ParentClass): def feature_3(self): print('feature_3 from ChildClass is running...') obj = ChildClass() obj.feature_1() obj.feature_2() obj.feature_3()
>> feature_1 from ParentClass is running...feature_2 from ParentClass is running...feature_3 from ChildClass is running...Multiple Inheritance
In this type of inheritance, one child class derives from two or more parent classes. Look at the following example code.
class ParentClass_1: def feature_1(self): print('feature_1 from ParentClass_1 is running...') class ParentClass_2: def feature_2(self): print('feature_2 from ParentClass_2 is running...') class ChildClass(ParentClass_1, ParentClass_2): def feature_3(self): print('feature_3 from ChildClass is running...') obj = ChildClass() obj.feature_1() obj.feature_2() obj.feature_3()
>> feature_1 from ParentClass_1 is running...feature_2 from ParentClass_2 is running...feature_3 from ChildClass is running...Multi-Level Inheritance
In this type of inheritance, the child class derives from a class which already derived from another class. Look at the following example code.class ParentClass: def feature_1(self): print('feature_1 from ParentClass is running...') class ChildClass_1(ParentClass): def feature_2(self): print('feature_2 from ChildClass_1 is running...') class ChildClass_2(ChildClass_1): def feature_3(self): print('feature_3 from ChildClass_2 is running...') obj = ChildClass_2() obj.feature_1() obj.feature_2() obj.feature_3()
>> feature_1 from ParentClass is running...feature_2 from ChildClass_1 is running...feature_3 from ChildClass_2 is running...Hierarchical Inheritance
In this type of inheritance, two or more child classes derive from one parent class. Look at the following example code.
class ParentClass_1: def feature_1(self): print('feature_1 from ParentClass_1 is running...') class ParentClass_2: def feature_2(self): print('feature_2 from ParentClass_2 is running...') class ChildClass(ParentClass_1, ParentClass_2): def feature_3(self): print('feature_3 from ChildClass is running...') obj = ChildClass() obj.feature_1() obj.feature_2() obj.feature_3()
>> feature_1 from ParentClass_1 is running...feature_2 from ParentClass_2 is running...feature_3 from ParentClass is running...Hybrid Inheritance
The hybrid inheritance is the combination of more than one type of inheritance. We may use any combination as a single with multiple inheritances, multi-level with multiple inheritances, etc.,
Cyclic Inheritance:
Two classes inheriting each other.
class P(Q):
passclass Q(P):
passMethod Resolution Order(MRO):
- It defines the class search path used by Python to search for the right method to use in classes having multi-inheritance.
- Priority: child --> left parent --> right parent
MRO Approachclass A:pass
class B:pass
class C:pass
class D(A):pass
class E(B):pass
class F(D,E,C):passF.mro()[__main__.F, __main__.D, __main__.A, __main__.E, __main__.B, __main__.C, object]
Comments
Post a Comment