Section 1/4Method Overriding
Method Overriding
When a child class defines a method with the same name as the parent, the child's version overrides the parent's. Python always calls the most specific version.
Polymorphism & Method Overriding
Polymorphism means "many forms". In OOP, the same method name can behave differently depending on which class's object you call it on. This lets you write code that works on a collection of related objects without knowing their exact types.
Method Overriding
When a child class defines a method with the same name as the parent, the child's version overrides the parent's. Python always calls the most specific version.
class Shape:
def area(self): return 0
class Rectangle(Shape):
def area(self): return self.width * self.height
class Circle(Shape):
def area(self): return math.pi * self.radius ** 2