
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
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 कहते है।
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
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
जब 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 का मिलेगा।
class Parent:
def show(self):
print("Parent ka show method")
class Child(Parent):
def show(self):
super().show() # Parent ka version call
print("Child ka show method")
c = Child()
c.show()
जब कोई 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
जब कोई 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
जब कोई 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
जब किसी 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
दो या दो से अधिक 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 करे,
Table of Contents All Chapters 1. Dunder Methods 2. Commonly Used Dunder Methods Python Tutorial Python Introduction Identation &...
Table of Contents All Chapters 1. Encapsulation 2. Access Modifiers 2.1. Public Members 2.2. Protected Members...
Table of Contents All Chapters 1. Lambda Function 2. Nestesd Lambda Function 3. Lambda Function with if-else 4. Lanbda with...
Table of Contents All Chapters 1. Nested Function 2. Return Function from Function 3. Higher Order Function 4. Function Recursion...
Table of Contents All Chapters 1. Parameters and Aguments 2. Function Parameters 2.1. *args 2.2. **kwargs...
Table of Contents All Chapters 1. Context Manager 2. with Statement 2.1. __enter__() Method 2.2. __exit__()...
Table of Contents All Chapters 1. Error Handling 2. Types of Error 2.1. Syntax Error 2.2....
Table of Contents All Chapters 1. File Pointer 2. File Pointer Methods 2.1. tell() Method 2.2....
Table of Contents All Chapters 1. File Handling 2. Types of File 3. open() and close() Methods 4. File Modes...