#PYTHON(DAY22)
object oriented programming(Inheritance)
In Python, object-oriented Programming (OOPs) is a programming paradigm that uses objects and classes in programming. It aims to implement real-world entities like inheritance, polymorphisms, encapsulation, etc. in the programming. The main concept of OOPs is to bind the data and the functions that work on that together as a single unit so that no other part of the code can access this data.
Main Concepts of Object-Oriented Programming (OOPs)
- Class
- Objects
- Polymorphism
- Encapsulation
- Inheritance
- Data Abstraction
we have already discussed about the class and objects in the previous story, now let us continue with the other concepts
Inheritance
Inheritance is the capability of one class to derive or inherit the properties from another class. The class that derives properties is called the derived class or child class and the class from which the properties are being derived is called the base class or parent class. The benefits of inheritance are:
- It represents real-world relationships well.
- It provides the reusability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
- It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
Types of Inheritance –
Single Inheritance:
Single-level inheritance enables a derived class to inherit characteristics from a single-parent class.
Example:
class parent:
px = 10 # static attribute
def parentMethod1(self):
print('This is method one from parent class')
class child(parent):
def childMethod1(self):
print('This is method one from child class')
p = parent()
c = child()
print(c.parentMethod1(),c.px)
And the output is:
This is method one from parent class
None 10
Multilevel Inheritance:
Multi-level inheritance enables a derived class to inherit properties from an immediate parent class which in turn inherits properties from his parent class.
Example:
class Gp:
def gpMethod(self):
print('Gp\'s method')
class P(Gp):
def parentMethod(self):
print('p\'s method')
class C(P):
def childMethod(self):
print('c\'s method')
x = C()
print(C.mro())
And the output is:
[<class '__main__.C'>, <class '__main__.P'>, <class '__main__.Gp'>, <class 'object'>]
Hierarchical Inheritance:
Hierarchical level inheritance enables more than one derived class to inherit properties from a parent class.
Example:
class P:
def pm(self):
print('this is a parent method')
class C1(P):
def c1m(self):
print('this is a child1 method')
class C2(P):
def c2m(self):
print('this is a child2 method')
C1.__mro__
And the output is:
(__main__.C1, __main__.P, object)
Multiple Inheritance:
Multiple level inheritance enables one derived class to inherit properties from more than one base class.
Example:
class Parent_1:
def parent1method(self):
print('this is from parent1')
class Parent_2:
def parent2method(self):
print('this is from parent2')
class Child(Parent_1,Parent_2):
pass
child = Child()
print(child.parent1method(),child.parent2method())
And the output is:
this is from parent1
this is from parent2
None None
No comments:
Post a Comment