Thursday 28 May 2015

Instance Control Flow In Parent And Child Relationship:

In my previous article I discussed about instace control flow in a class , static control flow in a class and static control flow in parent and child relationship. let see what will happen at the time of creating child class object.

Whenever we are creating child class object the following sequence of events will be executed automatically.

1) Identification of instance members from Parent to Child.
2) Execution of instance variable assignments and instance block only in Parent class.
3) Execution of Parent class constructor.
4) Execution of instance variable assignments and instance blocks in Child class.
5) Execution of Child class constructor.


I know the above program is bit confusing, I will try to elaborate more

At the time creating child object the following sequence of events will be performed automatically.

At first all instance members of parent and child are identified (just identified , no assignment of variables and execution of instance blocks is done), observe [1] to [11] .

After identification,variable assignment and execution of instace blocks starts only in parent class so value 5 is assigned to int firstNumber[12] , the next instance block is executed here firstMethod() method is called ,  observe  firstMethod() is trying to print secondNumber which is not yet initialized so the default value “0” is printed, now control flows back to instace block and the next statement  “Parent first instance Block”[15] is displayed on console. Now the secondNumber is initialized[16]. Next parent class constructor is executed [17].

Now,variable assignment and execution of instace blocks starts in child class so value 1 is assigned to int numberOne[18] , the next instance block is executed here secondMethod() method is called ,  observe  secondMethod() is trying to print numberTwo which is not yet initialized so the default value “0” is printed, now control flows back to instace block and the next statement  "Child first instance Block”[21] is displayed on console.

After it the second child instance block is executed and “Child Second instace block ” is printed[22], now the numberTwo is initialized[23]. Finally child constructor is being executed [24]. (Generally object initialization done in constructors). then main () method next statement will be executed[25].


// Instance control flow in Parent and Child relaitonship
package in.blogspot.java2bigdata;
/**
 * 
 *  @author Mahendhar E 
 */
public class ParentInstanceFlow {

 int firstNumber=5;
 {
  firstMethod();
  System.out.println("Parent first instance block");
 }
 ParentInstanceFlow()
 {
  System.out.println("parent class constructor");
 }
 public static void main(String[] args)
 {
  ParentInstanceFlow p=new ParentInstanceFlow();
  System.out.println("parent class main method");
 }
 public void firstMethod()
 {
  System.out.println(secondNumber);
 }
 int secondNumber=10;
}
class ChildInstanceFlow extends ParentInstanceFlow
{
 int numberOne=1;
 {
  secondMethod();
  System.out.println("Child first instance block");
 }
 ChildInstanceFlow()
 {
  System.out.println("Child class constructor");
 }
 public static void main(String[] args)
 {
  ChildInstanceFlow c=new ChildInstanceFlow();
  System.out.println("Child class main method");
 }
 public void secondMethod()
 {
  System.out.println(numberTwo);
 }
 {
  System.out.println("Child second instance block");
 }
 int numberTwo=2;
}



Output:
0
Parent first instance block
parent class constructor
0
Child first instance block
Child second instance block
Child class constructor
Child class main method


No comments:

Post a Comment