Prepare for the Java FPT Software Academy Test. Study with concise flashcards and multiple choice questions, each with detailed explanations. Get ready to excel in your exam!

Multiple Choice

In Java, when a subclass is instantiated, in what order are the constructors invoked?

When a subclass is instantiated, Java initializes the object from the top of the inheritance chain down. The constructors of all its superclasses run first, in order, and only after that does the subclass’s constructor body execute. This ensures inherited fields are properly initialized before the subclass adds its own state. So the superclass constructor runs first, then the subclass constructor. You can choose a specific superclass constructor with an explicit super(...) call, but it still executes before the subclass’s own initialization. If a superclass lacks an accessible no-arg constructor and you don’t call a superclass constructor explicitly, the code won’t compile.

When a subclass is instantiated, Java initializes the object from the top of the inheritance chain down. The constructors of all its superclasses run first, in order, and only after that does the subclass’s constructor body execute. This ensures inherited fields are properly initialized before the subclass adds its own state. So the superclass constructor runs first, then the subclass constructor. You can choose a specific superclass constructor with an explicit super(...) call, but it still executes before the subclass’s own initialization. If a superclass lacks an accessible no-arg constructor and you don’t call a superclass constructor explicitly, the code won’t compile.