Tracking stock after reference reassignment

A warehouse program uses the following Java classes to track the number of kits in a stock record:


Java
class Inventory {}

class KitStock extends Inventory {
    int kits;

    KitStock(int kits) {
        this.kits = kits;
    }
}


The following statements execute in order inside a method:


Java
Inventory current = new KitStock(24);
KitStock saved = (KitStock) current;
saved.kits -= 5;

current = new KitStock(40);
((KitStock) current).kits += 7;

Inventory earlier = saved;
((KitStock) earlier).kits += 3;


Determine the final number of kits accessed through saved, the final number of kits accessed through (KitStock) current, and the number of KitStock instances created by the shown new expressions. Give the first two quantities in kits and all three answers 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 Type Conversion and Casting Questions: