Relationships and dispatch
What is inheritance?
On this page
Inheritance lets one class acquire members or behaviours from another under the language’s lookup rules. It can serve reuse and subtyping, two different design aims. Before extending Queue, ask whether every promised queue operation still makes sense when clients receive your subclass.
Relationship diagram
Worked example
Pythonclass Queue:
def __init__(self):
self._items = []
def put(self, item):
self._items.append(item)
class InspectableQueue(Queue):
def pending(self):
return len(self._items)
jobs = InspectableQueue()
jobs.put("reindex")
assert jobs.pending() == 1Explore class relationships in the OOP Playground