Python Inheritance In Hindi

📔 : Python 🔗

किसी class को या Class की properties / behavior को extend करना Inheritance कहते हैं। जिस class को extend किया गया है उसे Parent Class , जिस class के द्वारा extend किया गया है उसे Child Class कहते हैं।


Extend करने पर Parent Class में present सभी properties / methods को child class द्वारा access किया जा सकता है।मतलब जो भी variables या methods parent class में होंगे बो child class में भी होंगे।

Inheritance Types

Python मुख्य रूप से तीन तरह की inheritance को support करती है।

  1. Single Inheritance.
  2. Multilevel Inheritance.
  3. Mulitple Inheritence.

Python Single Inheritance

Single Inheritance में किसी class द्वारा सिर्फ एक single class को ही Inherit किया जाता है। इस पूरे topic में आप single inheritance के बारे में ही पढ़ेंगे।

Python Inheritance Example

Python में किसी class को inherit करने के लिए simply class define करते समय parenthesis () में parent class ka name pass कर दिया जाता है।

Copy Fullscreen Close Fullscreen Run
# define parent class.
class ParentClass :
  # defining constructor.
  def __init__(self, fname, lname, age) :
    self.fname = fname 
    self.lname = lname 
    self.age = age 

  # function to print data.
  def print_info(self) :
    print('Full Name : ', self.fname, self.lname) 
    print('Age : ', self.age) 
  
# defining child class.
class ChildClass(ParentClass) :
  # just use pass statement because don't want to add anything in this class.
  pass

# initializing child class object to print data.
obj = ChildClass('Rahul', 'Kumar', 24)
obj.print_info()
Output
C:\Users\Rahulkumar\Desktop\python>python inheritance.py
Full Name :  Rahul Kumar
Age :  24

example में आप देख सकते हैं कि ParentClass में defined print_info() method को ChildClass Object द्वारा access किया गया है।

Note : pass keyword का use तब किया जाता है जब हमें class में कोई property या method को add नहीं करना हो।

Add __init__() method

अगर child class में कोई constructor नहीं है तो parent class का constructor child class का होगा , इसीलिए example में child class का Object बनाते समय data pass किया गया है।


और अगर child class में भी constructor (__init__()) method है तो parent class का constructor overwrite हो जाता है। फिर हमें manually parent class का constructor call करना होगा।


child class के अंदर parent class का कोई variable या method को super() method या parent class के name से access करते हैं।
See Example -

Copy Fullscreen Close Fullscreen Run
class ParentClass :
  def __init__(self, fname, lname, age) :
    self.fname = fname 
    self.lname = lname 
    self.age = age 

  def print_info(self) :
    print('Full Name : ', self.fname, self.lname) 
    print('Age : ', self.age) 

# defining child class.
class ChildClass(ParentClass) :
  def __init__(self, fname, lname, age) :
    # we can also use parent class name (ParentClass) instead of super().
    super().__init__(fname, lname, age)

# initializing child class object to print data.
obj = ChildClass('Rahul', 'Kumar', 24)
obj.print_info()
Output
C:\Users\Rahulkumar\Desktop\python>python inheritance.py
Full Name :  Rahul Kumar
Age :  24

Method Overriding

parent class के method को child class में overwrite करना ही method overriding कहलाता है।

Copy Fullscreen Close Fullscreen
class ChildClass(ParentClass) :
  # overwrite print_info() method.
  def print_info(self) :
    print('Full Name : ', self.fname, self.lname) 
    print('Age : ', self.age)
Output
C:\Users\Rahulkumar\Desktop\python>python inheritance.py
Full Name :  Rahul Kumar
Age :  24

चूंकि inheritance में parent class की सभी properties और methods child class से access किया जा सकता है। मलतब हम child class में current Object (जो कि method में default parameter होता है।) या super() method के through access कर सकते हैं।
For Example -

Copy Fullscreen Close Fullscreen Run
# parent class.
class Info :
  fname = 'Aryendra'
  lname = 'Sharma'
  age = 35

# child class.
class User(Info) :
  #method to print name.
  def get_name(self) :
    print('Full Name : ', self.fname, self.lname) 

  # method to print age, here we will access using super() method.
  def get_age(self) :
    print('Age : ', super().age) 

# initialize object and call function.
obj = User()
obj.get_name()
obj.get_age()
Output
C:\Users\Rahulkumar\Desktop\python>python inheritance.py
Full Name :  Aryendra Sharma
Age :  35

I Hope, अब Python में Inheritance के बारे में अच्छे से समझ गए होंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers