Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 81 fix this of a class that has no constructors in its source code You might think that coding such a constructor as in the Buffer class in this question is pointless However default constructors are a kind of emergency fallback as soon as any constructor is provided explicitly the default constructor goes away That can be surprising From a stylistic perspective it probably makes more sense to entirely avoid the default constructor If you want to be able to create an object with no explicit initialization write that constructor deliberately If you dont and incompletely initialized objects are probably bad for code reliability anyway dont let the compiler create it in the first place The call to the superclass constructor must happen in all classes except Object You dont always have to code it because if no explicit call to either super or this is in the code the compiler inserts the call to super with zero arguments exactly as is coded explicitly in the Buffer class Given this information you know that the Buffer class compiles correctly and option A is incorrect During object initialization the objects memory is first allocated for the entire object with data space allocated as needed for all the fields of all the classes in the hierarchy Then the initialization process starts by passing control up through the inheritance hierarchy to the Object that is always found at the top of the hierarchy This process is performed by the super calls at the start of every constructor Note that a call to this might replace a call to super but these amount to sidesteps in the same class and before any real initialization happens in this class a call to super will definitely take control up the hierarchy When control reaches the top java lang Object the body of the Object constructor is executed and then control returns down to the previous constructor That constructor is executed and returns to the next class down the hierarchy This process continues all the way back to the constructor on which new was invoked Finally when that constructor completes you have an initialized object As a side note no matter how many constructors are invoked in the hierarchy this process initializes the single object of the class on which new was called Of course other objects might be created during this process by explicit calls to new on other classes The essential observa
You must have JavaScript enabled to view digital editions.