This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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; } }
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
Joined: May 25, 2005
Posts: 30
posted
0
Only constructors makes a call to parent constructors - Do overridden methods also follow suit?
Geethakrishna Srihari
Ranch Hand
Joined: May 25, 2005
Posts: 30
posted
0
But why B is printed twice? Even if thats the case A followed by B shud be, and not B twice? Please clarify
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())
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.
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()?
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 !!..
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.
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
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
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.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.