In the Java program below, SmartPhone inherits from Phone, which inherits from Device, and each phone class also creates a part object in a field initialiser.
class Screen { Screen() { System.out.println("Screen"); } } class Battery { Battery() { System.out.println("Battery"); } } class Device { Device() { System.out.println("Device"); } } class Phone extends Device { private Screen screen = new Screen(); Phone() { System.out.println("Phone"); } } class SmartPhone extends Phone { private Battery battery = new Battery(); SmartPhone() { System.out.println("SmartPhone"); } }
The statement new SmartPhone(); runs once. Arrange the five printed lines in the order they appear on the console, first line first.
Loading ordering steps…
Unlock full course notes by signing up
Class counts for independent variations
A team compares two designs for software whose objects vary along independent dimensions. In the inheritance design, every combination of options gets its own concrete leaf subclass. In the composition design, the main class holds one component object per dimension, and every option is one concrete component class. For each system in the table, enter the number of concrete leaf subclasses the inheritance design needs and the number of concrete component classes the composition design needs. Do not count the main class or any abstract base classes, and enter whole numbers.
True statements about class reuse
A software team is reviewing its guidelines for reusing classes in an object-oriented program. Select every statement about composition and inheritance that is true.
Tracing a vehicle built from parts and subclasses
The Python classes below combine inheritance and composition. A Vehicle holds an engine object and delegates part of its speed calculation to it.
Give the value shown by each of the four print calls as a whole number.
Tracing a counting subclass
A programmer extends a simple collection class so that it counts how many items have been added:
What value does the program print? Give your answer as a whole number.
1.
Phone
2.
Battery
3.
Screen
4.
Device
5.
SmartPhone