What is the calling order of constructors along the hierarchy, from child up, or from ancestor down?
The order is from child up. However, the super() is always implicitly or explicitly called as the first statement before any code in the child being executed. This might cause some confusion here. If you put a print statement in each of the constructor, the ancestor one will be output first. The following code will illustrate how it works.This is called LIFO, which one is called the first, will be finished the last. The child is called first, but it call its parent as the first statement, and the immediate parent will call its parent as the first statement too, and so on. Therefore the far ancestor, which is the Object will finish its constructor the first, then its immediate child, and so on. That is why the print statements will finish from the ancestor down. The calling stack is working this way.
Related Questions
- Can a child WSUS server in a hierarchy get all the approvals from its parent server located at the main office but download the updates directly from the Windows Update site?
- Does iWise have the ability to show the hierarchy of relationships between parent and child configuration items?
- What is the hierarchy of payment when a child is covered by insurance and DSCC?