Types and contracts
What is method overriding?
On this page
A subtype overrides an inherited method by supplying a new implementation selected under the language’s method rules. A cached lookup can override get(key), but it must still honour the original contract for missing keys. Returning None where the base promises an exception can break existing callers.
Worked example
Pythonclass Directory:
def __init__(self, entries):
self.entries = entries
def lookup(self, key):
return self.entries[key]
class FoldedDirectory(Directory):
def lookup(self, key):
return super().lookup(key.casefold())
assert FoldedDirectory({"intro": 12}).lookup("INTRO") == 12Explore class relationships in the OOP Playground