Wednesday, 7 September 2016

Instance flow in inheritance

Instance context in Inheritance:-
In java, instance context is represented in the form of the following elements.
1.       Instance variable
2.       Instance method
3.       Instance blocks
Where instance variable and instance blocks are extend  automatically  just before executing respective class constructor. Where instance methods are recognized and executed at the time accessing that method.
In case of inheritance, when we access sub class constructor, first JVM will access 0-arg constructor in super class, after this only, JVM will execute sub class contactors.
In case of inheritance, if we provide instance context at each and every class then JVM will execute all the instance context just before executing their respective class constructors.

Ex code 1:-
class A
{
                A()
                {
                                System.out.println("A-Con");
                }
}
class B extends A
{
                B()
                {
                                System.out.println("B-Con");
                }
}
class C extends B
{
                C()
                {
                                System.out.println("C-Con");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                C c=new C();
                }
}
Output:-
A-Con
B-Con
C-Con

Ex code 2:-
class A
{
                A()
                {
                                System.out.println("A-Con");
                }
                {
                                System.out.println("IB-A");
                }
                int m1()
                {
                                System.out.println("m1-A");
                                return 10;
                }
                int i=m1();
}
class B extends A
{
                {
                                System.out.println("IB-B");
                }
                int j=m2();
                int m2()
                {
                                System.out.println("m2-B");
                                return 30;
                }
                B()
                {
                                System.out.println("B-Con");
                }
}
class C extends B
{
                C()
                {
                                System.out.println("C-Con");
                }
                int m3()
                {
                                System.out.println("m3-C");
                                return 40;
                }
                {
                                System.out.println("IB-C");
                }
                int k=m3();
}
class Test
{
                public static void main(String[] args)
                {
                                C c=new C();
                }
}
Output:-
IB-A
m1-A
A-Con
IB-B
m2-B
B-Con
IB-C
m3-c
C-Con

Ex code 3:-
class A
{
                A()
                {
                                System.out.println("A-Con");
                }
                static
                {
       System.out.println("SB-A");
    }
    int m1()
                {
                                System.out.println("m1-A");
                                return 10;
                }
                static int i=m2();
                {
                                System.out.println("IB-A");
                }
                int j=m1();
                static int m2()
                {
                                System.out.println("m2-A");
                                return 40;
                }
}
class B extends A
{
                B()
                {
                                System.out.println("B-Con");
                }
                {
                                System.out.println("IB-B");
                }
                static
                {
                                System.out.println("SB-B");
                }
                int m3()
                {
                                System.out.println("m3-B");
                                return 30;
                }
                static int m4()
                {
                                System.out.println("m4-B");
                                return 40;
                }
                int k=m3();
                static int l=m4();
}
class C extends B
{
                static
                {
                                System.out.println("SB-c");
                }
                static int m5()
                {
                                System.out.println("m5-C");
                                return 50;
                }
                static int m=m5();
                C()
                {
                                System.out.println("C-Con");
                }
                {
                                System.out.println("IB-C");
                }
                int m6()
                {
                                System.out.println("m6-C");
                                return 60;
                }
                int n=m6();
}
class Test
{
                public static void main(String[] args)
                {
                                C c1=new C();
                                System.out.println();
                                C c2=new C();
                }
}
Output:-
SB-A
m2-A
SB-B
m4-B
SB-C
m5-C
IB-A
m1-A
A-Con
IB-B
m3-B
B-Con
IB-C
m6-C
C-Con

IB-A
m1-A
A-Con
IB-B
m3-B
B-Con
IB-C
m6-C

C-Con

No comments:

Post a Comment