When the abstract superclass's constructor runs

A sensor library declares an abstract class Gauge and a concrete subclass TireGauge, as shown below. A program evaluates the expression new TireGauge(220.0). Put the initialization events for this new object in the order Java performs them.


Java
abstract class Gauge {
    protected String units = "kPa";
    protected final double reading;

    Gauge(double reading) {
        this.reading = reading;
        System.out.println("Gauge ready");
    }

    abstract double toDisplay();
}

class TireGauge extends Gauge {
    private int decimals = 1;

    TireGauge(double reading) {
        super(reading);
        System.out.println("TireGauge ready");
    }

    double toDisplay() {
        return reading;
    }
}
  1. 1.

    The TireGauge field initializer sets decimals to 1.

  2. 2.

    The Gauge field initializer sets units to "kPa".

  3. 3.

    The TireGauge constructor begins, and its first statement super(reading) passes control to the Gauge constructor.

  4. 4.

    Memory for the TireGauge object is allocated, with units, reading and decimals holding their default values.

  5. 5.

    The TireGauge constructor body prints TireGauge ready.

  6. 6.

    The Gauge constructor body stores the reading and prints Gauge ready.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Abstract Methods and Abstract Classes Questions: