Inheritance and Overriding Behaviour · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.
به انگلیسی

Borrow the shared parts, rewrite your own

Computing · Programming · ages 16-18
Name ______________________   Date ____________
  1. What does overriding an inherited method mean in these Python examples?

    • Deleting the parent's method for parent and child objects
    • Defining a subclass version used for subclass objects
    • Copying the unchanged parent method into every child object
  2. TourBus inherits Vehicle. Its initializer calls Vehicle's initializer, then sets self.seats = 28. Vehicle initializes wheels and defines refuel. What does the resulting TourBus have?

    • Initialized wheels and seats, plus inherited refuel
    • Only initialized seats, with no inherited refuel method
    • Initialized wheels and inherited refuel, but no seats
  3. Vehicle defines refuel. Bus inherits Vehicle and does not define refuel. In this single-parent example, bus.refuel() uses Vehicle's version.

    Circle one:   True   False

  4. Inside a TourBus.refuel override, which expression calls the inherited Vehicle.refuel before adding receipt text?

    • super().refuel(), then add the receipt text
    • super(), then treat the proxy as the receipt text
    • self.refuel(), then append the same receipt text
  5. Bus overrides trip_info but inherits refuel from Vehicle unchanged. A Bus object calls both. Which definitions handle the calls?

    • Vehicle trip_info and Vehicle refuel
    • Bus trip_info and Bus refuel
    • Bus trip_info and Vehicle refuel
  6. Ferry(Vehicle) defines __init__ that calls super().__init__() and assigns self.decks. It also defines its own refuel. Which change does this implement?

    • Adds self.decks and overrides refuel
    • Adds a class attribute and inherits refuel unchanged
    • Changes Vehicle itself and deletes its inherited methods
  7. Bird.fly() promises actual flight. Penguin overrides it to return success without flying. Penguin is still a bird. Which design avoids making it promise flight?

    • Keep the flight promise and return success without flying
    • Keep Bird general and put flight in a flying subtype
    • Keep the flight promise but rename Penguin's override
  8. RoadVehicle promises that change_lane() moves between road lanes. An Airplane model supports only airborne travel. It is proposed as a RoadVehicle child to reuse trip_info. What is the problem?

    • Sharing trip_info makes the road-lane contract optional
    • Adding its own fields cancels inherited method promises
    • Airplane cannot meet this road-only change_lane contract
LightMySky · lightmysky.comW1-mt_wBp7j_B6fj-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Borrow the shared parts, rewrite your own W1-mt_wBp7j_B6fj-s1

  1. Defining a subclass version used for subclass objects · A subclass defines its own version under the inherited method's name. The parent version still exists.
  2. Initialized wheels and seats, plus inherited refuel · The parent initializer creates wheels, the child assignment creates seats, and refuel is inherited.
  3. True · Bus has no replacement for refuel, so lookup reaches the inherited Vehicle method.
  4. super().refuel(), then add the receipt text · super() supplies the proxy. .refuel() selects and calls the inherited method. self.refuel() calls this override again.
  5. Bus trip_info and Vehicle refuel · The override handles trip_info. With no Bus replacement for refuel, that call uses Vehicle.
  6. Adds self.decks and overrides refuel · self.decks is an instance field. Ferry overrides both __init__ and refuel, while its initializer explicitly calls the inherited initializer.
  7. Keep Bird general and put flight in a flying subtype · Penguin can remain a Bird without inheriting a flight promise. A flying subtype can carry that capability.
  8. Airplane cannot meet this road-only change_lane contract · The stated Airplane model cannot perform the road-lane behaviour promised by RoadVehicle.
Worksheet · LightMySky