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 for Inheritance/Single Inheritance
class Base: "This is a Docstring(Optional)" Base_Class_Body class Derived(Base): "This is a Docstring(Optional)" Derived_Class_Body
Example For Inheritance in Python
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
issubclass() and isinstance() functions in Python
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







No comments: