Inheritance is the powerful feature of object oriented programming (oops).
it allows programmer to inherit any data or method from any class to another class i.e inheritance is way in which a class (known as child class) will use all the methods or functions defined in another class(parent class)
The new class (child class ) will include all the features of parent class and plus can contain some extra information
There are three types of inheritance :
Important
when defined in parent class only
class a:
def __init__(self):
print("boys")
class c(a):
pass
ob = c() # object of class c
inheriting __init__ function from parent class
when defined in parent and child class both
class a:
def __init__(self):
print("boys")
class c(a):
def __init__(self):
print("girls")
ob = c()
as we can see there is a init function already defined in class then it is not inheriting from parent class
it allows programmer to inherit any data or method from any class to another class i.e inheritance is way in which a class (known as child class) will use all the methods or functions defined in another class(parent class)
The new class (child class ) will include all the features of parent class and plus can contain some extra information
There are three types of inheritance :
- single inheritance
- multiple inheritance
- multilevel inheritance
Single inheritance
It is a type of inheritance in which a class can only inherit only from one parent class i.e there can be only one parent class and one child class
For example
class b(): # parent class
def vald(self):
print('my name is scarlett')
class d(b): #child class
def tan(self):
print("love")
s1 = d() # object of class d is made
s1.vald() # we are calling a function from class b through object of class d
s1.tan() # calling function
class b(): # parent class
def vald(self):
print('my name is scarlett')
class d(b): #child class
def tan(self):
print("love")
s1 = d() # object of class d is made
s1.vald() # we are calling a function from class b through object of class d
s1.tan() # calling function
Multiple inheritance
In this type of inheritance a (derived)child class can have more than one parent class . And inheritance from parent class is done in left to right approach i.e first , method from class A will be inherited and than from class B
for example:
class a:
def val(self):
print('my name is khan ')
class c:
def kar(self):
print("surila ")
class b(a,c): # child class inheriting both class a and class c
def vald(self):
print('my name is scarlett')
s1 = b()
s1.vald()
s1.kar() # object of class b is able to access method of class and class a
s1.val()
for example:
class a:
def val(self):
print('my name is khan ')
class c:
def kar(self):
print("surila ")
class b(a,c): # child class inheriting both class a and class c
def vald(self):
print('my name is scarlett')
s1 = b()
s1.vald()
s1.kar() # object of class b is able to access method of class and class a
s1.val()
MULTILEVEL INHERITANCE
multilevel inheritance is also possible in python unlike other programming language , in this a child class can inherit from another child class
for example :
class a:
def val(self):
print('my name is khan ')
class c(a): # inheriting from class a
def kar(self):
print("surila ")
class b(c): # inheriting from class c
def vald(self):
print('my name is scarlett')
class d(b): # inheriting from class b
def tan(self):
print("love")
s1 = d() # object of class d
s1.vald() # object of class d is able to call all the methods from diff classes
s1.kar()
s1.val()
s1.tan()
multilevel inheritance is also possible in python unlike other programming language , in this a child class can inherit from another child class
for example :
class a:
def val(self):
print('my name is khan ')
class c(a): # inheriting from class a
def kar(self):
print("surila ")
class b(c): # inheriting from class c
def vald(self):
print('my name is scarlett')
class d(b): # inheriting from class b
def tan(self):
print("love")
s1 = d() # object of class d
s1.vald() # object of class d is able to call all the methods from diff classes
s1.kar()
s1.val()
s1.tan()
- if there is a__init__ function or method defined in parent in parent and derived class both and the derived class is called then the priority will be given to the init function of its own class
when defined in parent class only
class a:
def __init__(self):
print("boys")
class c(a):
pass
ob = c() # object of class c
inheriting __init__ function from parent class
when defined in parent and child class both
class a:
def __init__(self):
print("boys")
class c(a):
def __init__(self):
print("girls")
ob = c()
as we can see there is a init function already defined in class then it is not inheriting from parent class




Comments
Post a Comment