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 a method mean?

    • deleting the parent method for all classes
    • writing a new version in the subclass used for its objects
    • copying the method into every object
  2. Class Bus extends class Vehicle and adds seats. What does a Bus object automatically have?

    • the Vehicle fields and methods plus seats
    • only the seats
    • nothing until rewritten
  3. If Bus does not override refuel, a Bus object calling refuel runs the Vehicle version.

    Circle one:   True   False

  4. An override of trip_info must reuse the parent text then add seats. What belongs inside it?

    • a call to super() plus the new seats line
    • a copy of the whole Vehicle class
    • a second class named Bus
  5. Bus overrides trip_info but inherits refuel. A Bus object calls both. Which versions run?

    • Vehicle trip_info and Vehicle refuel
    • Bus trip_info and Bus refuel
    • Bus trip_info and Vehicle refuel
  6. A programmer writes class Bus(Vehicle) with seats = 40 and a new trip_info. Which demand does this meet?

    • adds one field and overrides one method
    • adds one method and overrides one field
    • removes the parent entirely
  7. A programmer makes Penguin a subclass of Bird to reuse fly(), then overrides fly() to do nothing. What is the better call?

    • keep it, since overriding fixes everything
    • restructure so Penguin never promises flight it cannot do
    • delete fly() from every bird
  8. Car and Bus share Vehicle, but someone proposes adding Airplane under Vehicle to reuse trip_info. What is the flaw?

    • an airplane also travels, so nothing is wrong
    • subclasses may never add new fields
    • Vehicle promises road behaviour its other children share, which an airplane breaks
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. writing a new version in the subclass used for its objects · The subclass version replaces the parent one whenever a subclass object calls it.
  2. the Vehicle fields and methods plus seats · A subclass borrows everything from its superclass, then adds its own extras.
  3. True · Inherited methods run unchanged unless the subclass replaces them.
  4. a call to super() plus the new seats line · Calling super() runs the parent version so the override extends it instead of repeating it.
  5. Bus trip_info and Vehicle refuel · Each call runs the nearest version: the override for trip_info, the parent for refuel.
  6. adds one field and overrides one method · Seats is new data on the child, while trip_info replaces one parent behaviour.
  7. restructure so Penguin never promises flight it cannot do · An empty override hides a false promise. The family shape itself is what needs repair.
  8. Vehicle promises road behaviour its other children share, which an airplane breaks · A parent must fit every child, and road assumptions fail for an airplane.
Worksheet · LightMySky