কন্সট্রাকটর ইনহেরিট্যান্স | Object Oriented Programming

কন্সট্রাকটর ইনহেরিট্যান্স ক্লাসটি পলিটেকনিক [ Polytechnic ] এর অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং (বিষয় কোডঃ ৬৬৬৪১) [ Object Oriented Programming Code 66641 ] বিষয় এর অংশ।

 

কন্সট্রাকটর ইনহেরিট্যান্স

 

অবজেক্ট ওরিয়েন্টেড প্রোগ্রামিং এর সবচেয়ে গুরুত্বপূর্ণ বিষয়টি হচ্ছে ইনহেরি -টেন্স। ইনহেরিটেন্স অর্থ উত্তরাধিকার। প্রোগ্রামিং এ একটি ক্লাসের ডেটা অন্য ক্লাস উত্তরাধিকার সূত্রে ব্যবহার করার পদ্ধতিকে ইনহেরিটেন্স বলে ৷ যে ক্লাসের ডেটা ব্যবহার করা হয় তাকে প্যারেন্ট ক্লাস বা বেজ ক্লাস বলে। আর যে ক্লাস ডেটা ব্যবহার করে তাকে চাইল্ড ক্লাস বা ডিরাইভড ক্লাস বলে। ইনহেরিটেন্স প্রোগ্রামে রিডান্ডেন্সি কমায়, প্রোগ্রামকে লাইট ওয়েট করে।

 

কন্সট্রাকটর ইনহেরিট্যান্স

 

উদাহরণঃ

class Parent:

  def assets(self):
    print(“Assets of parents are inherited by child”)


class Child(Parent):
    pass
    
child1 = Child()
child1.assests()

আউটপুটঃ

Assets of parents are inherited by child

মাল্টিপল ইনহেরিটেন্স

একটি ক্লাস একই সাথে একাধিক ক্লাস কে ইনহেরিট করতে পারে। এটি পাইথনের একটি অন্যতম বৈশিষ্ট্য। সেক্ষেত্রে ডিরাইভড ক্লাসের প্যারেনথিসিস এর ভেতর প্রতিটা বেজ ক্লাসের নাম উল্ল্যেখ করে দিতে হয়। একে মাল্টিপল ইনহেরিটেন্স বলে।

 

উদাহরণঃ

class Father:

  def hero(self):
    print("Every father is a hero")
    
class Mother:
  
  def warrior(self):
    print("Every mother is a warrior")
    
class Child(Father,Mother):
    
   def life(self):
     print("Child’s life is inherited from Father and Mother")
      
we = Child()
we.life()
we.hero()
we.warrior()

আউটপুটঃ

Child’s life is inherited from Father and Mother

Every father is a hero

Every mother is a warrior

 

মাল্টিলেভেল ইনহেরিটেন্স

যদি একটি ডিরাইভড ক্লাসের ও আরও ডিরাইভড ক্লাস থাকে তখন একে মাল্টি লেভেল ইনহেরিটেন্স বলে।

কোন ক্লাস কোন ক্লাসের চাইল্ড ক্লাস বা কোনটি কোন ক্লাসের অবজেক্ট তা চেক করার জন্য issubclass() ও isinstance() দুটো বিল্ট ইন ফাংশন ব্যবহার করা হয়।

isinstance() ফাংশনটি দুটো প্যারামিটার নেয়। প্রথমটি হল অবজেক্ট,দ্বিতীয়টি একটি ক্লাস এবং এটি বুলিয়ান ভ্যালু রিটার্ন করে। যদি অবজেক্ট টি ওই ক্লাসের হয় তাহলে True অন্যথায় False রিটার্ন করে।

 

উদাহরণঃ

class Grand:

  def ancestor(self):
    print("I am the Grand Class")
    
class Parent(Grand):
  
  def second_generation(self):
    print("I am inherited from Grand Class")
    
class Child(Parent):
    
    def new_generation(self):
      print("We are third generation")

we = Child()
we.new_generation()
we.second_generation()
we.ancestor()

কন্সট্রাকটর ইনহেরিট্যান্স এর বিস্তারিত ঃ

 

 

Leave a Comment