What does it mean to override a method?


#1

Question

What does it mean to override a method?

Answer

The point of overriding is to allow different behavior from the same function in different classes.
In our Employee and PartTimeEmployee example, we want both employees to be paid, so we create a method to calculate their wages. And since a part time employee is definitely a type of Employee, it makes sense for them to derive a lot of the same functionality.
However! That doesn’t mean their pay should be calculated in the same way. So we override the calculate_wage method to behave as we want it to for PartTimeEmployee objects.
By defining a method with the same name as found in the base class, Employee, we are overriding the functionality so that any PartTimeEmployees will have their wages calculated appropriately.


Syntax FAQ: Learn Python - Introduction to Bitwise Operators - Override!