Output order in a three-level constructor chain

The three Java classes below form an inheritance chain. A program creates one object with new Thermometer(); and prints nothing else.


Java
class Device {
    Device() {
        System.out.println("Device: power on");
    }
}

class Sensor extends Device {
    Sensor(int id) {
        System.out.println("Sensor: id " + id + " assigned");
    }
}

class Thermometer extends Sensor {
    static int nextId() {
        System.out.println("Thermometer: id requested");
        return 7;
    }

    Thermometer() {
        super(nextId());
        System.out.println("Thermometer: calibrated");
    }
}


Arrange the four lines of output in the order the program prints them, from first to last.

  1. 1.

    Device: power on

  2. 2.

    Thermometer: id requested

  3. 3.

    Thermometer: calibrated

  4. 4.

    Sensor: id 7 assigned

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 Inheritance Questions: