instance initializer
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
public class Test
{
int i=fun();
int j=5;
int fun(){
System.out.println("A");
return 1;}
public static void main(String[] args)
{
System.out.println("X");
Test t=new T();
System.out.println("Y");
System.out.println(t.i);
System.out.println(t.j);
}
}
class T extends Test
{
int i=fun();
int j=10;
int fun()
{
System.out.println("B");
return 2;
}
}
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
---------------
Test t=new T();
------------------
So, when you instantiate the subclass, it instantiate the parent class first. At that time it runs fun() (overridden in sub class which prints B)
and when the sub class instantiates again it prints B.
Hope it clears your doubt.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Geethakrishna Srihari:
Only constructors makes a call to parent constructors - Do overridden methods also follow suit?
An overriden method does not, by default, call the method that it is overriding. However, you can call the super class' method, by using the super keyword. (e.g. super.fun())
Henry
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Geethakrishna Srihari:
But why B is printed twice? Even if thats the case A followed by B shud be, and not B twice? Please clarify
The reason B is being printed twice, is because the fun() method is being called twice. Notice that there are two i variables, that you are initializing.
Henry
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am getting really confused here.int fun() is overriden in T(subclass)....so ,if one wants to call the fun() method from subclass T it would be resovled depending upon the type of actual object...this is run time polymorphism...right!!!...but when you want to refer the method fun() from superclass Test why does it call the sub class fun()?
Shweta Dhaneshwar.<br />SCJP 1.4 90%
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The above code reflects that before initialising child class instance variables it goes to initialise parent.
This occurs coz before creating the child instance , parent class is loaded as well as its instance is created !!..
Thnx
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Although we are initializing t in the Test's class, it is the T instance we are assigning to the Test reference variable(t). So, we are instantiating the subclass.
Try with this example for more clarification.
Hope this will clear your doubt.
Thanks,
Lalitha.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Can you please explain in detail?
"There are no mistakes, only lessons"
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
http://www.javaranch.com/maha/Discussions/Language_Fundamentals/Constructor_-_JavaRanch_Big_Moose_Saloon.htm
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Amol Fuke:
I did not get the explanation of above code .
Can you please explain in detail?
Read my code... the lady who has posted after my reply has messed up the explaination
Thnx
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Nischal Tanna:
The above code reflects that before initialising child class instance variables it goes to initialise parent.
This occurs coz before creating the child instance , parent class is loaded as well as its instance is created !!..
the code by nischal tanna ..produces the following results
D:\java_prac>java Test
Inside static of parent
X
Inside static of Child
B
Inside Test
2
B
Inside Child
2
Y
can anybody pls explain the step by step..how it printed
i got most of the logic but stuck
that how 2 is printed after Inside Test
??
Thanks and Regards, Amit Taneja
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
{
System.out.println("Inside Test");
System.out.println(i);
}
this block of code gets executed before this code
{
System.out.println("Inside Child");
System.out.println(i);
}
and then program prints
B // int i=fun(); of child class
Inside Child // first line of fun() of child class
2 // second line of fun() of child class
Piyush Jain<br /> <br />Being happy doesn't mean everything's perfect. It means you've decided to see beyond the imperfections.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| Where all the women are strong, all the men are good looking and all the tiny ads are above average: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











