Inheritance
Method Overriding
Replacing and extending parent method behavior
Interview: Core polymorphism concept — tests understanding of override vs overload, LSP, and extension patterns
Method overriding occurs when a subclass provides a specific implementation of a method already defined in its parent class. The overridden method in the child class replaces the parent's version when called on child objects. This is the foundation of polymorphism in OOP.
Overriding vs Overloading
- Overriding: Same method name, different class (child replaces parent) — supported in Python
- Overloading: Same method name, different parameters — NOT supported in Python (use defaults/*args)
- Overriding is resolved at runtime (dynamic dispatch)
- The method signature should match (or be compatible with) the parent
Extension vs Replacement
You can either completely replace a parent method or extend it by calling super() within the override. Extension preserves parent behavior while adding child-specific logic.
Interview Tip
Know when NOT to override: if the child behavior is completely different (not a specialization), inheritance may be wrong. Consider composition instead.
Use Cases
Polymorphic behavior: different shapes calculating area differently
Template method pattern: parent defines algorithm skeleton, children override steps
Customizing framework behavior (Django views, Flask handlers)
Adding logging, validation, or caching to parent methods
Plugin systems: plugins override hooks from base plugin class
Common Mistakes
Changing the method signature when overriding (breaks Liskov Substitution Principle)
Forgetting to call super() when extending (not replacing) parent behavior
Overriding methods that shouldn't be overridden (e.g., __init__ without super())
Not understanding that overriding is dynamic — the actual method called depends on the object type
Using override when composition would be cleaner