Posted By Naveen Singh on 20 May 2023
python-program
Object oriented programming (OOP) language में inheritance एक basic concept है। यह एक mechanism है जो आपको classes की एक hierarchy बनाने की अनुमति देता है जो किसी अन्य
classes में से एक class प्राप्त करके उसमे इन properties और methods का एक set share करता है। Inheritance एक class की दूसरे class से properties को प्राप्त या inherit करने की क्षमता रखता
है।
Syntax-:
Class ParentClass:
{
body
}
Class ChildClass(ParentClass):
{
body
}
Parent class वह class होती है जिसकी properties को child class inherit करता है|
Example-:
class Student(object):
# Constructor
def _init_(self, name, id):
self.name = name
self.id = id
# To check if this Student is an boy
def Show(self):
print(self.name, self.id)
# Driver code
obj = Student("Harrison", 22) # An Object of Student
obj.Show()
Output-:
Harrison 22
Child class वह class होती है जो कि parent class की properties को inherit करती है।
Example-:
class New_Stu(Student):
def Result(self):
print("Student class called")
Stu_details = New_Stu("Aryan", 10)
# calling parent class function
Stu_detail.Show()
# Calling child class function
Stu_detail.Result()
Output-:
Aryan 10
Student class called
class Student(object):
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getStuName(self):
return self.name
# To check if this person is an employee
def isPassed(self):
return False
# Inherited or Subclass (Note Student in bracket)
class Boys(Student):
# Here we return true
def isPassed(self):
return True
# Driver code
obj = Person("Student1") # An Object of Student
print(obj.getStuName(), obj.isPassed())
obj2 = Boys("Student2") # An Object of Boys
print(obj2.getStuName(), obj2.isPassed())
Student1 False
Student2 True
एक child class को यह identify करने की जरूरत है कि कौन सी class उसकी parent class है। यह child class की परिभाषा में parent class के name को mension करके किया जा सकता है।
Example-:
class childclass_name(parentclass_name)
# parent class
class Student(object):
# __init__ is known as the constructor
def __init__(self, name, rollnumber):
self.name = name
self.rollnumber = rollnumber
def show(self):
print(self.name)
print(self.rollnumber)
# child class
class Record(Student):
def __init__(self, name, rollnumber, course, year):
self.course = course
self.year = year
# invoking the __init__ of the parent class
Student.__init__(self, name, rollnumber)
# creation of an object variable or an instance
obj = Record('Ayush', 101, “Java”,2022)
# calling a function of the class Person using its instance
obj.show()
Output-:
Ayush
101
super() function एक तरह का built-function होता है जो parent class को represent करने वाले object को return करता है। यह child class में parent class के methods और attributes
को access करने की भी अनुमति देता है।
Example-:
# parent class
class Candidate():
def _init_(self, name, age):
self.name = name
self.age = age
def show(self):
print(self.name, self.age)
# child class
class children(Candidate):
def _init_(self, name, age):
self.Name = name
self.Age = age
# inheriting the properties of parent class
super()._init_("Harrison", age)
def showInfo(self):
print(self.Name, self.Age)
obj = children("David", 18)
obj.show()
obj.showInfo()
Output-:
Harrison 18
David 18