What is Inheritance ?
Inheritance ek esa machanism hai jo hume ek new class banane ke liye allow karta hai , jise Child class ke naam se jana jata hai Jo pehle se bani ek Parent class par based hoti hai.
Inheritance waha kaam aata hai jaha hume bahut similar classes banane ki jarurat hoti hai. aapko sirf itna karne ki jarurat hai ki Parent class me aap wo code likho jo dono classes me common hai aur jo code dono classes me specific hai wo aap Child class me likho. Is se hume bahut sara code likhna nahi padta matlb duplicating of code se bach jate hai.
Ise hum ek example se samjhte hai….
# class First
class Lion:
def eat(self):
print("The Lion eats food.")
def roar(self):
print("The lion is roaring")
# class second
class Dog:
def eat(self):
print("The dog eats food.")
def bark(self):
print("The dog is barking.")
l = Lion() #create lion class object
l.eat() # call eat method of lion class by lion class object
l.roar() # call roar method of lion class by lion class object
print() #change line (blank line)
d = Dog()
d.eat()
d.bark()
Is example me Two Animal class hai – (i) Lion (ii) Dog . Dono classes me eat(self) function common hai. Other practicle programs me classes code multiple lines ka hota hai. ese me agar aap apni classes ke function me kuch update karna chaho to aapko one by one sabhi classes ke function me change karna padega. Itna hard work karne se hume jo save karta hai…wo hai Inheritance Concept. Inheritance se hum dono classes ki common properties ko ek general Animal class(Parent class) me abstract kar sakte hai Then hum jitni chahe utni child classes bana sakte hai… jese – Lion, Dog and Cow jo Animal class se inherit hogi. ek child class apni parent class ke sabhi attributes and methods inherit kar sakti hai and khood ke attributes and methods bhi add kar sakti hai.
Parent class par based child class banane ke liye following syntax use karte hai :
class ParentClass:
# body of Parent Class
# method 1
# method 2
class ChildClass(ParentClass):
# body of Child Class
# method 1
# method 2
Object-Oriented concepts me, Class2 ko jab Class1 se inherit kiya jata hai.. to hum kehte hai ki Class2 extand class hai Class1 ki ya fir Class2 derived class hai Class1 ki.
ab hum inheritance concept use kar upper wale example ki ek parent class (Animal) or do child class ( Lion and Dog) create karte hai…
#Parent Class
class Animal:
def eat(self, name):
print("The",name,"eats food.")
# Child class of Animal
class Lion(Animal):
def roar(self):
print("The lion is roaring.")
# Child class of Animal
class Dog(Animal):
def bark(self):
print("The dog is barking.")
l = Lion() # create Lion class object
l.eat("lion") # Lion class object can call parent class methods
l.roar()
print() # blank line or change line
d = Dog()
d.eat("dog")
d.bark()
Outputs:
The lion eats food.
The lion is roaring.
The dog eats food.
The dog is barking.

Multiple Inheritance
Python me hum ek bar me ek Class ko several classes se derive kar sakte hai. yeh multiple inheritance ke naam se jana jata hai. iska genral format kuch is trha se hai :
Class ParentClass_1:
# body of Parent Class_1
Class ParentClass_2:
# body of Parent Class_2
Class ParentClass_3:
# body of Parent Class_3
Class ChildClass(ParentClass_1, ParentClass_2, ParentClass_3):
# body of Child Class
Yaha ChildClass …. ParentClass_1, ParentClass_2 , ParentClass_3 ki derived class hai. As a result, yeh tino class ke sabhi attributes and methods ko inherit karegi .
example :
class A:
def example1(self):
print("exmaple1() method called")
class B:
def example2(self):
print("exmaple2() method called")
class C:
def example3(self):
print("exmaple3() method called")
class D(A, B, C):
def test(self):
print("test() method called")
d_obj = D()
d_obj.example1()
d_obj.example2()
d_obj.example3()
d_obj.test()
Output :
example1() method called
example2() method called
example3() method called
test() method called