Python | Inheritance, Multi-Level Inheritance & Multiple Inheritance By. Shivansh Sir

 PYTHON PROGRAMMING

PYTHON | INHERITANCE

Inheritance OOP(Object-Oriented Programming) का एक हिस्सा है | Inheritance में एक से ज्या classes होते है |

Inheritance में दो प्रकार के मुख्य class देना अनिवार्य होता है |

Base Class (Parent) : Base class को super या parent class भी कहा जाता है |

Derived Class (Child) : Derived class को sub या child class भी कहा जाता है | Derived class ये Base class की properties या attributes को inherit करता है |

Syntax

class Base:

"This is a Docstring(Optional)"

Base_Class_Body

class Derived (Base):

"This is a Docstring(Optional)"

Derived_Class_Body

Example

Example पर Fitness Class के obj इस object से Employee Class की properties को inherit किया गया है |

Source Code 

class Employee:

"Class Employee"

    def set1(self,empid,name,salary):        

        self.empid = empid

        self.name = name

        self.salary = salary

class Fitness(Employee):

    "Class Fitness"

def set2(self,height,weight):

        self.height = height

        self.weight = weight

    def display(self):

        print("id is",self.empid)

        print("name is",self.name)

        print("salary is",self.salary,"Rs")

        print("height is",self.height,"cm")

        print("weight is",self.weight,"Kg")

obj = Fitness()

obj.set1(1,"Rakesh",27000)

obj.set2(176,60)

obj.display()

Output

id is 1

name is Rakesh

salary is 27000 Rs

height is 176 cm

weight is 60 Kg

PYTHON | INHERITANCE | issubclass() & isinstance() functions

issubclass(subclass, superclass)

isinstance(object, class)

issubclass() ये function दिए हुए superclass का दिया हुआ subclass है या नही ये boolean value में return करता है |

isinstance() ये function दिए हुए class का दिया हुआ object है या नही ये boolean value में return करता है |

Source Code

class Employee:

    pass

class Fitness(Employee):

    pass

obj1 = Fitness()

obj2 = Employee()

print(issubclass(Fitness, Employee))

print(isinstance(obj1, Fitness))

print(isinstance(obj2, Fitness))

Output

True

True

False

PYTHON | INHERITANCE | MULTILEVEL INHERITANCE

Multilevel Inheritance में एक base class और दो derived class होते है | Multilevel Inheritance में derived class का एक base class होता है और derived class का भी एक derived class होता है |

Syntax

class Base:

Base_Class_Body

class Derived1(Base):

Derived1_Class_Body

class Derived2(Derived1):

Derived2_Class_Body

Example

- Employee class के Fitness और Company ये दो derived class है | उसके बाद Fitness इस class का एक base class(Employee) और एक derived class(Company) है |

- उसके बाद Company इस class के Fitness और Employee ये दो base class है | Company class ये Fitness और Employee की properties को inherit करता है |

- सिर्फ Company class का object Fitness और Employee इन दोनों की properties को access करता है |

Source Code

#Base class of Fitness and Company

class Employee:

    def set1(self,empid,name,salary):        

        self.empid = empid

        self.name = name

        self.salary = salary

#Base class of Company and Derived class of Employee

class Fitness(Employee):

    def set2(self,height,weight):

        self.height = height

        self.weight = weight

#Derived class of Fitness and Employee

class Company(Fitness):

    def set3(self,company,dep):

        self.company = company

        self.dep = dep

    def display(self):

        print("id :",self.empid)

        print("name :",self.name)

        print("salary :",self.salary,"Rs")

        print("height :",self.height,"cm")

        print("weight :",self.weight,"kg")

        print("Company :",self.company)

        print("Department :",self.dep)

obj = Company()

obj.set1(1,"Rakesh",27000)

obj.set2(176,60)

obj.set3("Infosys", "IT")

obj.display()

Output

id : 1

name : Rakesh

salary : 27000 Rs

height : 176 cm

weight : 60 kg

Company : Infosys

Department : IT

PYTHON | INHERITANCE | MULTIPLE INHERITANCE

Multiple Inheritance में एक से अधिक base classes हो सकते है और एक derived class होता है | Derived class को दो या दो से ज्यादा base classes को inherit किया जाता है, उसे Multiple Inheritance कहते है | Multiple Inheritance में एक Child Class और उसके एक से ज्यादा Parent Classes को inherit करता है |

Syntax

class Base1:

Base1_Class_Body

class Base2:

Base2_Class_Body

class Derived(Base2,Base1):

Derived_Class_Body

Example

Employee और Fitness ये दो Base class है और उनका एक ही derived class है जो दोनों base class की properties को inherit करता है |

Source Code

#Base class of Company

class Employee:

    def set1(self,empid,name,salary):        

        self.empid = empid

        self.name = name

        self.salary = salary

#Base class of Company

class Fitness:

    def set2(self,height,weight):

        self.height = height

        self.weight = weight

#Derived class of Fitness and Employee

class Company(Fitness,Employee):

    def set3(self,company,dep):

        self.company = company

        self.dep = dep

    def display(self):

        print("id :",self.empid)

        print("name :",self.name)

        print("salary :",self.salary,"Rs")

        print("height :",self.height,"cm")

        print("weight :",self.weight,"kg")

        print("Company :",self.company)

        print("Department :",self.dep)

obj = Company()

obj.set1(1,"Rakesh",27000)

obj.set2(176,60)

obj.set3("Infosys", "IT")

obj.display()

Output 

id : 1

name : Rakesh

salary : 27000 Rs

height : 176 cm

weight : 60 kg

Company : Infosys

Department : IT

For Any Doubt Clear on Telegram Discussion Group

Join Us On Social Media

For Any Query WhatsApp Now

Comments

Popular posts from this blog

Python | All in One Program By. Shivansh Sir

Python | Data Types | Number & it's Conversion By. Shivansh Sir

Python | Loop | For Loop By. Shivansh Sir