Pergunta de entrevista da empresa Trantor

If class A extends class B, which class will be called first and why?

Resposta da entrevista

Sigiloso

14 de dez. de 2024

If class A extends class B, the constructor of class B (the parent class) will be called first when an object of class A is created. This is because the child class (A) relies on the initialization of its parent class (B) before it can be fully constructed. Why? Java ensures that the parent class's properties and methods are initialized before the child class. The first statement in a child class's constructor is an implicit or explicit call to the parent class's constructor using super().