9 4 Inheritance Explained
Key Concepts
Inheritance in Python involves several key concepts:
- Base Class and Derived Class
- Method Overriding
- Super Function
- Multiple Inheritance
- Inheritance and Polymorphism
1. Base Class and Derived Class
Inheritance allows a class to inherit attributes and methods from another class. The class that is inherited from is called the base class or parent class, and the class that inherits is called the derived class or child class.
Example:
class Animal: def __init__(self, name): self.name = name def speak(self): pass class Dog(Animal): def speak(self): return f"{self.name} says Woof!" dog = Dog("Buddy") print(dog.speak())
Analogy: Think of inheritance as a parent-child relationship where the child inherits traits from the parent.
2. Method Overriding
Method overriding occurs when a derived class provides a specific implementation of a method that is already defined in its base class. This allows the derived class to customize behavior.
Example:
class Animal: def speak(self): return "Animal sound" class Cat(Animal): def speak(self): return "Meow!" cat = Cat() print(cat.speak())
Analogy: Think of method overriding as a child choosing to do something differently than their parent.
3. Super Function
The super()
function is used to call a method from the parent class. This is useful when you want to extend the functionality of a parent method in the derived class.
Example:
class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound" class Dog(Animal): def __init__(self, name, breed): super().__init__(name) self.breed = breed def speak(self): return f"{super().speak()} and says Woof!" dog = Dog("Buddy", "Golden Retriever") print(dog.speak())
Analogy: Think of the super()
function as a way to consult the parent before making a decision.
4. Multiple Inheritance
Multiple inheritance allows a class to inherit from more than one base class. This can be useful for combining features from different classes.
Example:
class Flyer: def fly(self): return "Flying" class Swimmer: def swim(self): return "Swimming" class Duck(Flyer, Swimmer): pass duck = Duck() print(duck.fly()) print(duck.swim())
Analogy: Think of multiple inheritance as a child inheriting traits from both parents.
5. Inheritance and Polymorphism
Polymorphism allows objects of different classes to be treated as objects of a common superclass. This is often used with inheritance to create flexible and reusable code.
Example:
class Animal: def speak(self): pass class Dog(Animal): def speak(self): return "Woof!" class Cat(Animal): def speak(self): return "Meow!" def animal_sound(animal): print(animal.speak()) dog = Dog() cat = Cat() animal_sound(dog) animal_sound(cat)
Analogy: Think of polymorphism as a universal remote that can control different devices, each with its own specific functions.
Putting It All Together
By understanding and using these concepts effectively, you can create more organized and reusable Python code.
Example:
class Animal: def __init__(self, name): self.name = name def speak(self): return f"{self.name} makes a sound" class Dog(Animal): def __init__(self, name, breed): super().__init__(name) self.breed = breed def speak(self): return f"{super().speak()} and says Woof!" class Cat(Animal): def speak(self): return "Meow!" def animal_sound(animal): print(animal.speak()) dog = Dog("Buddy", "Golden Retriever") cat = Cat("Whiskers") animal_sound(dog) animal_sound(cat)