A developer creates a Python class with a method that is intended to be overridden in subclasses. Which approach best ensures that the method is not accidentally called on the base class?
Trap 1: Use 'pass' as the method body
pass does nothing and allows accidental calls.
Trap 2: Delete the method from the base class using 'del'
Deleting the method would prevent any call, including from subclasses.
Trap 3: Add a comment '# override in subclass' inside the method body
Comments do not enforce overriding.
- A
Use 'pass' as the method body
Why wrong: pass does nothing and allows accidental calls.
- B
Delete the method from the base class using 'del'
Why wrong: Deleting the method would prevent any call, including from subclasses.
- C
Add a comment '# override in subclass' inside the method body
Why wrong: Comments do not enforce overriding.
- D
Raise NotImplementedError inside the method body
Raising NotImplementedError clearly signals the method must be overridden.