Inheritance in Python in Hindi

What is Inheritance in Python

Inheritance किसी भी Object Oriented Programming का core features होता है जो allow करता है कि एक class किसी दूसरे class के attributes और methods को inherit कर सकता है। ये code की reusability और classes के hierarchy build करने मे help करता है। वो class जिसमे मे inherit किया जाता है उसे parent class कहते है और वो class जो inherit करता है उसे child class कहते है।

Key Concepts of Inheritance

Base Class (Parent Class): ये वो class है जिसमे से attributes और methods को inherit किया जाएगा। 

Derived Class (Child Class): ये वो class है जो base class से inherit करेगा। 

Overriding: जब child class का method अपने parent class के method से redefine हो जाता है तो इसे overriding कहते है, ऐसा तब होता है जब दोनों class मे same method exists करते है।  

Super() Function: A function used to call a method from the parent class inside the child class.

SYNTAX : 

class BaseClass:

       # Base class definition 

class DerivedClass(BaseClass):

       # Derived class definition

Super Function

जब child class का method या attribute अपने parent class के method या attribute से redefine हो जाता है तो इसे overriding कहते है, ऐसा तब होता है जब दोनों class मे same name method या attribute exists करते है। ऐसे मे हम Super() function का use करते है, अगर हमे child class के object से किसी ऐसे attribute या method का access करेंगे जो parent class मे भी हो तो हमे child class का ही मिलेगा but जब हम super() function के साथ उस attribute या method को access करेंगे तो parent class का मिलेगा।

Types of Inheritance

Single Inheritance

जब कोई child class सिर्फ एक parent class से inherit करता है तो ऐसे inheritance को Single Inheritance कहते है।

SYNTAX : 

class Parent:

    def method(self):

        print(“Parent method”)

class Child(Parent):

    pass

child = Child()

child.method()  # Output: Parent method

Multiple Inheritance

जब कोई child class एक से अधिक parent class से inherit करता है तो ऐसे inheritance को Single Inheritance कहते है।

SYNTAX : 

class Base1:

       # Base1 class definition

class Base2:

       # Base2 class definition

class Derived(Base1, Base2):

       # Derived class definition

Multilevel Inheritance

जब कोई child class ऐसे class से inherit करता है जो खुद किसी parent class से inherit किया रहता है तो इसे Multiple Inheritance कहते है।

SYNTAX : 

class Grandparent:

    def method(self):

        print(“Grandparent method”)

class Parent(Grandparent):

    pass

class Child(Parent):

    pass

child = Child()

child.method()  # Output: Grandparent method

Hierarchical Inheritance

जब किसी parent class से multiple child classes inherit करते है तो इसे Hierarchical Inheritance कहते है।

SYNTAX : 

class Parent:

    def method(self):

        print(“Parent method”)

class Child1(Parent):

    pass

class Child2(Parent):

    pass

child1 = Child1()

child2 = Child2()

child1.method()  # Output: Parent method

child2.method()  # Output: Parent method

Hybrid Inheritance

दो या दो से अधिक type के inheritance के combination को Hybrid Inheritance कहते है।

SYNTAX : 

class A:

    def method_A(self):

        print(“Method A”) 

class B(A):

    def method_B(self):

        print(“Method B”)

class C(A):

    def method_C(self):

        print(“Method C”)

class D(B, C):

    def method_D(self):

        print(“Method D”)

d = D()

d.method_A()  # Output: Method A

d.method_B()  # Output: Method B

d.method_C()  # Output: Method C

d.method_D()  # Output: Method D

उम्मीद करते है कि आपको Python Classes and Objects का एक useful concept Inheritance अच्छे समझ मे आ गया होगा । अपने learning को continue रखने के लिए next button पर click करे,

Add a comment...

  • All Posts
  • Artificial Intelligence
  • Computer Fundamentals
  • Computer Networks
  • Data Analytics
  • Data Science
  • DBMS
  • Deep Learning
  • Digital Electronics
  • DSA with Python
  • Excel
  • Exercise
  • Git & Github
  • Machine Learning
  • Matplotlib
  • Natural Language Processing
  • NumPy
  • Operating System
  • Pandas-s
  • Power BI
  • Python Tutorial
  • Scikit-learn
  • Seaborn
  • SQL & MySQL
computer networks

Table of Contents All Chapters 1. OSI Model      1.1. Physical Layer      1.2. Data Link Layer  ...

computer networks

Table of Contents All Chapters 1. Topology 2. Types of Topology      2.1. Point to Point Topology    ...

computer networks

Table of Contents All Chapters 1. Data Communication 2. Types of Data Communication      2.1. Simplex      2.2....

digital electronics number system

Table of Contents All Chapters 1. Logic Gate 2. AND Gate      2.1. AND Gate Truth Table 3. OR...

Edit Template

Sign up to track your learning

Login to continue your learning tracking

To reset your password, please enter your email address or username below.

Scroll to Top