Extending a class
22-minute lesson · module-inheritance-02
You are previewing this course as a guest
Read anything and run any exercise you like. It all runs in your own browser, so nothing here is saved: no XP, no progress, and the AI tutor needs an account. Sign in and your work starts counting.
Extending a class
22-minute lesson · module-inheritance-02
- Define a subclass with `class Sub(Base):` so it inherits the base's methods.
- Call the base constructor with `super().__init__(...)` from a subclass.
- Add new attributes and methods specific to the subclass.
- Override a base method and optionally extend it via `super()`.
Source attribution
Status: adapted
How to Think Like a Computer Scientist: Interactive Edition — source · Inheritance / Extending the Base Class
Adaptation notes. Reading rewritten in our voice. A CampusMember base class is extended by a GradStudent subclass that adds an adviser and overrides describe(). Hidden tests check that inherited attributes are set via super().__init__ and that the override works.
License notes. Original chapter is GNU FDL 1.3 (Brad Miller and David Ranum). No verbatim text from the original is reproduced here.