Java Magazine, July/August 2018
ORACLE COM JAVAMAGAZINE JULY AUGUST 2018 86 fix this mentation of the abstract method of the same signature declared in Car The implementation is public where the abstract method has default access but thats OK An overriding or implementing method must not be less accessible You cannot instantiate an abstract class but you can use an abstract class or interface as a variable type and you can assign objects that are built from subclasses or implementations to that variable than the method it overrides but it is fine for it to be more accessible In other words there are zero abstract methods in RacingCar so line n2 would not cause any problems The use of the abstract base class Car as the type of the variable c in line n3 would also be completely correct You cannot instantiate an abstract class but you can use an abstract class or interface as a variable type and you can assign objects that are built from subclasses or implementations to that variable This kind of generalization is one of the reasons that abstract classes exist to allow you to write code in terms of generalizations the abstract class or interface that will work correctly with any specialization that is with an object created from a concrete subclass or implementation At this point you can be confident that the code would not have compilation issues at either line n2 or line n3 Therefore if the code with the abstract version of Car would have compiled correctly how would it have behaved To answer this you need to consider how the construction process works and that aspect is fairly simple The constructors run from the top of the class hierarchy Object downward This means that the RacingCar constructor would run after the Car constructor Because of that the initial value of speed would be 180 rather than 90 These speeds are in kilometers per hour presumably If the initial speed is 180 the speed printed after the accelerate method is called would be 230 Theres a potentially interesting side discussion about how exam questions are usually phrased As a rule questions are intended to be specific about the line containing the problem that causes compilation to fail Recently written questions generally dont suggest a cause
You must have JavaScript enabled to view digital editions.