Assume you have the following two classes: public class Bag { } private String name; public Bag() { name= "Fancy bag"; }
Posted: Fri Jul 08, 2022 6:43 am
Assume you have the following two classes: public class Bag { } private String name; public Bag() { name= "Fancy bag"; } public class Backpack extends Bag { private int noCompartments; // number of compartments in a backpack public Backpack() { noCompartments = 1; } public int getNoCompartments() { return noCompartments; } } Assume that you have a variable of type Bag initialized to an object instance of type Backpack, as follows: Bag bag = new Backpack(); Show a fragment of code to print the number of compartments in a Bag referenced by the variable bag, as assigned above.