Tuesday, 1 May 2018


Instance control flow in parent to child relationship:----------------

·         Identification of instance members parent to child
·         Execution of instance variable assignments and instance blocks only in parent class.
·         Execution of Parent constructor.
·         Execution of instance variable assignments and instance blocks in Child class.
·         Execution of Child constructor.

==================================================================

public class Parent {
            int x=10;
            {
                        m1();
                        System.out.println("Parent instance block");
            }
           
            Parent()
            {
                        System.out.println("Parent constructor");
            }
           
            public static void main (String args[])
            {
                        Parent p=new Parent();
                        System.out.println("Parent main");
            }
           
            public void m1()
            {
                        System.out.println(j);
            }
           
            int j=20;
}

==========================================================================



public class Child extends Parent {
           
            int x=100;
            {
                        m2();
                        System.out.println("Child first instance block");
            }
           
            Child()
            {
                        System.out.println("Child constructor");
            }
           
            public static void main(String[] args) {
                        Child c=new Child();
                        System.out.println("Child main");
                       
            }
           
            public void m2()
            {
                        System.out.println(y);
            }
            {
                        System.out.println("Child second instance block");
            }
           
            int y=200;

}

==================================================================================

Run  Child  class:----

o/p:--------------------------

0
Parent instance block
Parent constructor
0
Child first instance block
Child second instance block
Child constructor
Child main

No comments:

Post a Comment