Home >> Python Tutorial >> Python Inheritance
Edit Template
Edit Template

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

🐍
Example 1.py
Copy to clipboard
class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        return "Some sound"

class Dog(Animal):
    def speak(self):
        return "Bark"

# Creating an object of the derived class
dog = Dog("Buddy")
print(dog.name)       # Output: Buddy
print(dog.speak())    # Output: Bark

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 Fundamentals
  • 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
Python Inheritance

Table of Contents All Chapters 1. Inheritance 2. Concepts of Inheritance 3. Types of Inheritance      3.1. Single Inheritance...

Python Function

Table of Contents All Chapters 1. Python Function 2. Define Function 3. Function Arguments 4. Function Parameters 5. Local and...

Python Dictionary

Table of Contents All Chapters 1. Python Dictionary 2. Access Dictionary items 3. Change Dictionary items 4. Add items in...

Python Set

Table of Contents All Chapters 1. Python Set 2. Iterate Set items 3. Set Methods      3.1. Add items...

Python Tuple

Table of Contents All Chapters 1. Python Tuple 2. Access Tuple Items 3. Update Tuple 4. Iterate Tuple Items 5....

Python List

Table of Contents All Chapters 1. Python List 2. Nested List 3. Range List 4. List Indexing 5. list Methods...

Python String

Table of Contents All Chapters 1. Python String 2. String Indexing 3. String Slicing 4. String Concatenation 5. String Formatting...

Python Loops

Table of Contents All Chapters 1. Python Loops 2. While Loop      2.1. Nested While Loop 3. For Loop...

Edit Template
Scroll to Top