Updating shared behavior without editing subclasses

A print shop uses the following Java code to calculate daily paper use. Each return value is a number of sheets. The multiplication in HandoutJob.dailySheets() represents two separate, identical print batches.


Java
class PrintJob {
    public int sheetsFor(int copies) {
        return 2 * copies + 1;
    }
}

class PosterJob extends PrintJob {
    public int dailySheets() {
        return sheetsFor(8) + sheetsFor(3);
    }
}

class HandoutJob extends PrintJob {
    public int dailySheets() {
        return 2 * sheetsFor(5);
    }
}

class LabelJob extends PrintJob {
    public int dailySheets() {
        return 2 * 6 + 1;
    }
}


The shop changes only the body of PrintJob.sheetsFor() to:


Java
return 3 * copies + 2;


All the code is then recompiled. Calculate the new return value of dailySheets() for one PosterJob, one HandoutJob, and one LabelJob. Also calculate the increase in their combined daily sheet count compared with the original code. Enter all four answers in sheets as exact integers.

Your answers

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: