Python Inheritance Overview

Python class me bhi Inheritance ka use usi tarah hota hai jaisa C++, Java, Dotnet me hota hai, difference hai to sirf syntax ka. Inheritance class ka ek important part hai, jiske through ek class ke properties aur methods ko dusri class me apply kiya ja sakta hai. 

Inheritance me  parent class aur child class ka concept hai. Koi bhi child class parent class ki properties aur methods ko call kar sakta hai. Parent class ko base class aur child class ko derived class bhi bola jata hai.

Parent class ko kaise create karen?

#GENERAL EXAMPLE
class ParentClassName:

    def __init__(self, first_argument, second_argument):
        self.first_arg = first_argument
        self.second_arg = second_argument

    def print_method(self):
       print(self.first_arg, self.second_arg)

obj = ParentClassName(“First”, “Second”)
obj.print_method()

Niche diye Example me Student naam ka ek Parent Class or Base Class hai. jahan hum student ka naam uski age ko stu1 object ke through print kara rahe hain.

jaise hi stu1 object create hoga waise hi def__init__() function initialize hoga aur object ke through bheji gai information ko __init__() ke argument ke sath match karega. Baad me stu1 object printdetails() function ke through student ka naam aur uski age print kar dega.

# EXAMPLE  – for class Student 
class Student:

    def __init__(self, name,age):
        self.stu_name = name
        self.stu_age = age

    def printdetails(self):
       print(self.stu_name, self.stu_age)

stu1 = Student(“Suraj”, 15)
obj.printdetails()

Child class ko kaise create karen?

Python Class Inheritance me child ko create karne ka easy step hai. Jis kisi bhi parent class ki properties aur methods ko child class access karna chata hai, uss parent class ko child class ka parameter bana diya jata hai. Example se samajhiye:

# Child Class Syntax 
class ChildClass(ParentClass):
    pass

# EXAMPLE  – for child class GirlStudent 
class Student:
     #parent Class

    def __init__(self, name,age):
        self.stu_name = name
        self.stu_age = age

    def printdetails(self):
        print(self.stu_name, self.stu_age)

class GirlStudent(Student):   #Child class
    def girldetail(self):
        print(self.stu_name + ” is a girl student of class and her age is”,+self.stu_age)

stu1 = Student(“Suraj”, 15)
stu1.printdetails()
stu2 = GirlStudent(“Salini”, 15)
stu2.girldetail()

GirlChild ek child class hai aur yah Student ki properties aur methods ko access kar raha hai. Aap dekh pa rahe honge ki child class self.stu_name aur self.stu_age ki value ko easily access kar raha hai.

# EXAMPLE  – for child class GirlStudent 
class Student:
     #parent Class

    def __init__(self, name,age):
        self.stu_name = name
        self.stu_age = age

    def printdetails(self):
        print(self.stu_name, self.stu_age)

class GirlStudent(Student):   #Child class
    def girldetail(self):
        print(self.stu_name + ” is a girl student of class and her age is”,+self.stu_age)

stu1 = Student(“Suraj”, 15)
stu1.printdetails()
stu2 = GirlStudent(“Salini”, 15)
stu2.girldetail()

Inheritance me function Overriding kya hota hai?

Inheritance me hum parent class ki kuch properties aur methods ko define karte hain aur uss parent class ka child class uske poore properties aur methods ko inherit karega.

Yaha tak to sahi hai lekin kabhi-kabhi child class ko khud ki properties aur methods ko use karna hota hai aur jab koi object child class ko call karta hai tab child class apni defined properties ko implement karta hai. Iss type ke cases ko Function Overriding kahte hain. Example se samajhte hain:

# Function Overriding
class Student:     #parent Class
    def __init__(self, name,age):

        self.stu_name = name
        self.stu_age = age
    def printdetails(self):
        print(self.stu_name, self.stu_age)

class GirlStudent(Student):
    def __init__(self, fullname,herage):
        self.stu_name = fullname
        self.stu_age = herage

    def girldetail(self):
        print(self.stu_name + ” is a girl student of class and her age is”,+self.stu_age)

stu2 = GirlStudent(“Salini”, 15)
stu2.girldetail()

Kyuki humne __init__() function child class me bhi add kar diya hai issliye object create hote hi child class ko point karega aur uske data ko print karke dega. 

# Function Overriding
class Student:     #parent Class
    def __init__(self, name,age):

        self.stu_name = name
        self.stu_age = age
    def printdetails(self):
        print(self.stu_name, self.stu_age)

class GirlStudent(Student):
    def __init__(self, fullname,herage):
        Student.__init__(self, name,age)
        self.stu_name = fullname
        self.stu_age = herage

    def girldetail(self):
        print(self.stu_name + ” is a girl student of class and her age is”,+self.stu_age)

stu2 = GirlStudent(“Salini”, 15)
stu2.girldetail()

stu1 = Student(“Saurav”, 15)
stu1.printdetails()

Inheritance me Super() function kya hai?

jab kisi bhi child class ko parent class ke properties aur methods ko inherit karna hota hai, uss time me super() function bhut hi important role play karta hai. 

Suppose karte hain ki “Student” ek parent class hai aur wahi “GirlStudent” ek child class hai. Ab hum chahte hain ki child class super() function ka use karke parent class ke properties aur methods ko inherit kare. isse example se samajhte hain: 

# Super() Function ka concept

class Student:                             
    def __init__(self, name,age):
        self.stu_name = name
        self.stu_age = age
    def printdetails(self):
        print(self.stu_name, self.stu_age)

class GirlStudent(Student):
    def __init__(self,fullname,herage):
    super().__init__(fullname,herage)

x = GirlStudent(“Lissa”, 21)
x.printdetails() 

Super() function ka use karte waqt hume parent class to mention nhi karna padta balki yah function automatically parent class ki functionalities ko inherit kar leta hai.  

yah 2 line samajhne wali hain: 

(1) super().__init__(fullname,herage)
yaha hum “GirlStudent” child class ke andar se super() function ka use karke “Student” class ke property aur methods ko inherit kar rahe hain. 

(2) x = GirlStudent(“Lissa”,21)   
yaha x child class ka object hai. yah object initiate hote hi child class ko call karta hai aur arguments(“Lissa”,21) ko parameters(fullname,herage) se assign kar deta hai. Ab jaise hi super() function call hoga to turant hi values(“lissa”,21) parent class ke parameters(name,age) se assign hokar parent class ke method (printdetails) ko execute kar dega.