This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

instance initializer

 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is B printed twice between X and Y:

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;
}
}
 
Chitra AP
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because you have overridden the fun() method in the subclass.

---------------
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.
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only constructors makes a call to parent constructors - Do overridden methods also follow suit?
 
Geethakrishna Srihari
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why B is printed twice? Even if thats the case A followed by B shud be, and not B twice? Please clarify
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shweta R Dhaneshwar
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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()?
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 !!..
 
Lalitha Gottumukkula
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shweta,

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.
 
Amol Fuke
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get the explanation of above code .
Can you please explain in detail?
 
soumya ravindranath
Ranch Hand
Posts: 300
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

??
 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 is printed after inside Test bcoz parent class is initialize first before the child class and
{
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
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an excellent chapter on initialization in Bruce Eckel's Thinking in Java available free online. Should explain all you need to know about object initialization.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic