Force Subclasses to Implement pay()

Hard ⏱ 18 min 32% acceptance ★★★★☆ 4.2
Using abc.ABC and @abstractmethod, define PaymentMethod with abstract pay(amount) — so instantiating it raises TypeError — and a concrete subclass UpiPayment implementing pay to return f'paid {amount} via UPI'. ABCs turn "you forgot to override" from a runtime surprise into an instantiation error.

Examples

Example 1
Input
PaymentMethod()
Output
TypeError
Explanation

Abstract classes cannot be instantiated.

Example 2
Input
UpiPayment().pay(250)
Output
'paid 250 via UPI'
Explanation

The concrete subclass fulfils the contract.

Constraints

  • Inherit from ABC.
  • Mark pay with @abstractmethod.

Topics

OOPabstract base classes

Companies

StripePayPalAdyen

Hints

Hint 1

from abc import ABC, abstractmethod.

Hint 2

The subclass only needs the one method.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.