Menu
CoddyTech

Inheritance and super

Lesson 39 of 48 in Coddy's Python for JavaScript Developers course.

Extend an existing class when the new type is a specialized form of it, while reusing initialization and behavior that still apply. Start with single inheritance so the relationship and method lookup remain straightforward.

JavaScript

class Message {
  constructor(text) { this.text = text; }
  render() { return this.text; }
}
class LoudMessage extends Message {
  render() { return super.render() + "!"; }
}
console.log(new LoudMessage("Hi").render());

Python

class Message:
    def __init__(self, text):
        self.text = text
    def render(self):
        return self.text
class LoudMessage(Message):
    def render(self):
        return super().render() + "!"
print(LoudMessage("Hi").render())

What changes

Both programs print Hi!. Python places the base class in parentheses after the subclass name. The subclass inherits methods it does not replace. Its own render overrides the inherited version, while super().render() reuses the base behavior in this single-inheritance example. Because this subclass has no initializer, it inherits the base initializer. If it defines __init__ to accept additional data, explicitly call super().__init__(...) to establish base state before storing the extra fields.

Common mistake

Calling self.render() from inside the same override calls the override again, not the base method. Use super() for that delegation. Also avoid inheritance solely to borrow a helper: composition is often clearer when one object uses another but is not a specialized form of it.

Your task

Extend a regular pass with discount behavior. The locked driver checks the subclass relationship, base attributes, and recalculation after the stored price changes.

challenge icon

Challenge

Beginner

Implement Pass(label, price) with stored attributes and cost() returning the integer price. Implement subclass DiscountPass(label, price, discount), initializing base state through super().__init__ and storing discount. Override cost() to return the base cost minus the discount, clamped at zero. Input is label, nonnegative price, and nonnegative discount on separate lines. The driver prints the label and cost for a regular pass and a discounted pass, then increases the discounted pass's price by 10 and prints its new cost. Calculate from current state.

The locked driver handles input and output. Submit only the required class and function definitions with the exact API names. Do not read input or print from your classes.

The locked driver also verifies the required subclass relationship at runtime.

Your code goes in the editable files pass_model.py and discount_pass.py. driver.py holds the supplied input/output code and API checks and cannot be edited. Keep the supplied imports; do not read input or print from the editable modules.

Try it yourself

from pass_model import Pass
from discount_pass import DiscountPass

assert issubclass(DiscountPass, Pass), "Required subclass relationship missing"
label = input()
price, discount = int(input()), int(input())
a, b = Pass(label, price), DiscountPass(label, price, discount)
print(a.label, a.cost())
print(b.label, b.cost())
b.price += 10
print(b.cost())
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Python for JavaScript Developers

Practice on your own: Online Python compiler