Q1. What is inheritance in Python?
Inheritance allows a class (child) to acquire attributes and methods from another class (parent). It promotes code reuse. Example:
class Animal:
def speak(self):
print("Animal sound")
class Dog(Animal):
def speak(self):
print("Bark")Q2. How do you call the parent class constructor?
Use super().__init__() in the child class to call the parent's __init__. Example:
class Child(Parent):
def __init__(self, arg1, arg2):
super().__init__(arg1)
self.arg2 = arg2Q3. What is method overriding?
Method overriding occurs when a child class defines a method with the same name as a parent method. The child's implementation replaces the parent's when called on an instance of the child. super() can be used to call the parent method from the child.
Q4. What is multiple inheritance?
Multiple inheritance allows a class to inherit from more than one parent class. Python supports it. Example:
class A: pass
class B: pass
class C(A, B): pass Method resolution order (MRO) determines which parent method is used.Q5. What is the purpose of the super() function?
super() returns a proxy object that delegates method calls to a parent or sibling class. It is used to call parent methods without explicitly naming the parent class, making code more maintainable, especially in multiple inheritance scenarios.
