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

InspectableQueuepending()Queueput(item)inherits from

Worked example

Python
class 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() == 1

Python tutorial on classes

Explore class relationships in the OOP Playground

What is method overriding?How do modern languages approach object orientation?What is a method?

All OOP questions

Search ObjectFAQ