উত্তরাধিকার ৷ ধারণা যেখানে একটি শ্রেণী অন্য শ্রেণীর পদ্ধতি এবং বৈশিষ্ট্য অ্যাক্সেস করে।
- প্যারেন্ট ক্লাস হল সেই শ্রেণী যা থেকে উত্তরাধিকারসূত্রে প্রাপ্ত হয়, যাকে বেস ক্লাসও বলা হয়।
- শিশু শ্রেণী হল সেই শ্রেণী যা অন্য শ্রেণী থেকে উত্তরাধিকারসূত্রে প্রাপ্ত হয়, যাকে ডেরাইভড ক্লাসও বলা হয়।
পাইথন -
-এ দুই ধরনের উত্তরাধিকার রয়েছে- একাধিক উত্তরাধিকার
- মাল্টিলেভেল ইনহেরিটেন্স
একাধিক উত্তরাধিকার -
একাধিক উত্তরাধিকারে একটি শিশু শ্রেণি একাধিক অভিভাবক শ্রেণির উত্তরাধিকারী হতে পারে।
উদাহরণ
class Father:
fathername = ""
def father(self):
print(self.fathername)
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
class Daughter(Father, Mother):
def parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
s1 = Daughter()
s1.fathername = "Srinivas"
s1.mothername = "Anjali"
s1.parent() আউটপুট
Father : Srinivas Mother : Anjali
মাল্টিলেভেল ইনহেরিটেন্স
এই ধরনের উত্তরাধিকারে, একটি শ্রেণী একটি শিশু শ্রেণী/উত্পন্ন শ্রেণী থেকে উত্তরাধিকারী হতে পারে।
উদাহরণ
#Daughter class inherited from Father and Mother classes which derived from Family class.
class Family:
def family(self):
print("This is My family:")
class Father(Family):
fathername = ""
def father(self):
print(self.fathername)
class Mother(Family):
mothername = ""
def mother(self):
print(self.mothername)
class Daughter(Father, Mother):
def parent(self):
print("Father :", self.fathername)
print("Mother :", self.mothername)
s1 = Daughter()
s1.fathername = "Srinivas"
s1.mothername = "Anjali"
s1.family()
s1.parent() আউটপুট
This is My family: Father : Srinivas Mother : Anjali