Order of Execution with Inheritance in Java

Order of Execution with Inheritance in Java

aaaaa.png

When we run new Subclass() then then sections of the code are executed in the following sequence

  1. In Superclass, all static variables are initialized and all static initialization blocks are run, in order in which they appear in the file.
  2. In Subclass, all static variables are initialized and static initialization blocks are run, in the order in which they appear in the file.
  3. In Superclass, all instance variables are initialized and all object initialization blocks are run, in the order in which they appear in the file.
  4. In Superclass, the (relevant) constructor is run.
  5. In Subclass, all instance variables are initialized and all object initialization blocks are run, in the order in which they appear in the file.
  6. In Subclass, the (relevant) constructor is run.