π¨βπ¦ Inheritance¶
Inheritance allows a class (the child or derived class) to inherit attributes and methods from another class (the parent or base class).
This promotes code reusability. Instead of rewriting logic, you extend it!
𧬠Basic Inheritance¶
To inherit from a class, you put the parent class name in parentheses when defining the child class.
class Animal:
def eat(self):
print("Munch munch...")
class Dog(Animal): # Dog inherits from Animal
def bark(self):
print("Woof!")
d = Dog()
d.eat() # Inherited from Animal!
d.bark() # Defined in Dog
π¦Έ The super() Function¶
When you want to use the parent's method but also add something extra, you use super(). This is very common in the __init__ method.
class Animal:
def __init__(self, name):
self.name = name
class Dog(Animal):
def __init__(self, name, breed):
# Call the parent's __init__ to handle the name
super().__init__(name)
self.breed = breed
π Overriding Methods¶
A child class can provide a different implementation for a method that is already defined in its parent class. This is called method overriding.
class Cat(Animal):
def eat(self):
print("The cat eats gracefully.")
c = Cat("Whiskers")
c.eat() # Output: The cat eats gracefully.
π³ Multi-level Inheritance¶
You can inherit from a class that inherits from another class.
Animal -> Mammal -> Dog
πΈοΈ Multiple Inheritance¶
Python supports inheriting from multiple classes at once (less common, can be complex).
π Key Takeaways¶
- Inheritance: Creates a hierarchy (Parent -> Child).
- Reusability: Child classes get parent features for free.
super(): Access parent methods.- Overriding: Change inherited behavior in the child class.
isinstance(obj, Class): Check if an object belongs to a class or its parents.