2011-12-06

Compiler and JVM activities of a java program with inheritance

Compiler Activities:

While compiling a class with inheritance relationship, compiler first compiles the super class and then it compiles the sub class
In finding method or variables definitions, compiler always first search in sub class. If it is not available in sub class, it searches in immediate super class. If there also it is not available compiler throws CE: "cannot find symbol"
This phenomenon is called local preference.

JVM Activities:

JVM also executes the invoked members from sub class, if that member definition is not available in sub class,JVM executes it from super class
Execution control flow in inheritance:
when sub class is loaded its entire super classes are loaded, and also when sub class object is created all its super class memory is created.
What is the order of execution of class members when we invoke them using sub class object ?

1. Class loading order is from super class to sub class. Static variables and static blocks execution is from super class to sub class
If you want to know that then type
java -verbose:class subclass-name in command prompt
and see the loading order of classes first super class is loaded into JVM and then subclass is loaded. Super class is loaded into JVM using extends keyword.
2. Object creation is from super class to sub class
Non static variables and non static blocks and constructor execution is from super class to sub class. Super class object is created by calling super class constructor from sub class constructor using super() keyword. Actually speaking object is not created for super class only memory is created.
3. Execution starts from sub class to super class
Invoked method is executed from sub class . If it is not defined in sub class, it is executed from super class

//Below program explains above points

//Example.java
class Example 
{
   static int a = m1();

   //static block starts
   static
   {
      System.out.println("Example SB - Example is loaded");
   }
   //static block ends

   int x = m2();

   //non-static block starts
   {
      System.out.println("Example-NSB");
   }
   //non-static block ends

   Example(){
      System.out.println("Example class constructor-object is created");
   }
   static int m1(){
      System.out.println("Example static variable is created");
      return 10;
   }
   int m2(){
      System.out.println("Example non-static varible is executed");
      return 20;
   }
   void abc(){
      System.out.println("Example abc");
   }
   void bbc(){
      System.out.println("Example bbc");
   }
}

//Sample.java
class Sample extends Example 
{
   static int b = m3();

   //static block starts
   static
   {
      System.out.println("Sample SB - Sample is loaded");
   }
   //static block ends

   int x = m4();

   //non-static block starts
   {
      System.out.println("Sample-NSB");
   }
   //non-static block ends

   Sample(){
      System.out.println("Sample class constructor-object is created");
   }
   static int m3(){
      System.out.println("Sample static variable is created");
      return 30;
   }
   int m4(){
      System.out.println("Sample non-static varible is executed");
      return 40;
   }
   void abc(){
      System.out.println("Sample abc");
   }

   public static void main(String[] args) 
   {
      System.out.println("main method");
      Sample s = new Sample();
      s.abc();
      s.bbc();
   }
}

In the above program first static variables and static blocks of super class (Example)  are executed and then the static variables and static blocks of sub class (Sample) are executed. If all the static variables and blocks execution is completed then the main method is executed which is present in Sample class. After that object creation statement is executed control passes to Sample constructor and then passes to Example constructor due to the super() keyword that is placed by the compiler during compilation time.The super class of all classes is Object class so Object class statements are executed then Example class object is created means non-static variables and non-static blocks of Example class are executed then Example constructor statements are executed. So object creation of Example class is completed. Then non-static variables and non-static blocks of Sample class are executed  and then Sample constructor statements are executed. Object creation of Sample class is completed. Finally invoked methods from sub class to super class are executed. This is the control flow of inheritance program.

The output for the above program is:

Example static variable is created
Example SB - Example is loaded
Sample static variable is created
Sample SB - Sample is loaded
main method
Example non-static varible is executed
Example-NSB
Example class constructor-object is created
Sample non-static varible is executed
Sample-NSB
Sample class constructor-object is created
Sample abc
Example bbc

Loading

Enter your Email here: