Browse OOP reference
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

Python
class 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") == 12

Python tutorial on classes

Explore class relationships in the OOP Playground

What is inheritance?What is method overloading?What is duck typing?

All OOP questions

Search ObjectFAQ