GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructors" Watch "constructors" New topic
Author

constructors

Susmita Pal
Ranch Hand

Joined: Jul 12, 2001
Posts: 31
class A {
public int Avar;
public A() {
System.out.println("AAA");
doSomething();
}
public void doSomething() {
Avar = 1111;
System.out.println("A.doSomething()");
}
}
public class B extends A {
public int Bvar = 2222;
public B() {
System.out.println("BBB");
doSomething();
System.out.println("Avar=" + Avar);
}
public void doSomething() {
System.out.println("Bvar=" + Bvar);
}
public static void main(String[] args) {
new B();
}
}

the Output is
AAA
Bvar=0 //Specially this output
BBB
Bvar=2222
Avar=0
Can somebody please explain me the output.
Thanks in adv.
Susmita


Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
what has happened is doSomethign function in class B has overridden the doSomething function in class A. so when doSomething function is called from the construtor of A, doSomething function(of class B) is called and at that point in time variable BBB has not been initialized its valu; so it prints 0
correct me if i am wrong
vidhya subramaniam
Ranch Hand

Joined: Jul 14, 2001
Posts: 91
Namrata,
i didnt understand your explanation. Why does it take the value of Bvar as 0, since it has already been declared right in the beginning of the class as public and given a value 2222.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
See the lines numbers and explanation.

// 1 the first thing is super class constructor is called.
//2 if A has super class then it is called, in this case Object class's no-arg constructor is called
which does nothing.
// 3 then the instance variables are intiazied, in this case Avar's values are set to 1111 in doSomething method so here Avar is set to 0
//4 prints AAA
//5 doSomething() method is overridden, so at runTime, it calls the method in class B
//6 at this time, the instance variables are not intialized in Class B so the default value 0 is printed
//7 now it prints Avar = 0, because it didn't exeute the method doSomething () in A
// 8 now the instance variables in Class B are intialized, Bvar = 2222
// 9 Then it calls the doSomething Method in class B, it prints, Bvar = 2222
//10 now it prints the value of AVar = 0
Hope this helps,
Vanitha

[This message has been edited by Vanitha Sugumaran (edited July 23, 2001).]
Ravindra Mohan
Ranch Hand

Joined: Mar 16, 2001
Posts: 216
Hi Folks,
Let me take a shot at this.
This question should be seen in the light of :
1. Process and order of initialization of creation of objects. : Class variables are loaded first that is, static variables are initialised. Final variables come next.Instance variables are initialised later and finally constructor is called.
For example look at the fol :


2. Method overiding.:
When you refer a to derived class object by the reference of base class then the method call is determined by the type of the Object (ie, the derived class's object).
Now as regards to this question.
Instance variable of <code> class A </code> viz., <code>AVar</code> is set to default "0".
You create an of the derived class,so the constructor of base class is implicitely called by a call to <code>super(); </code> as the first statement in the derived classes constructor. So, we get the display "AAA". After this is done,the call to <code> doSomething() </code> is depends
upon the type of object (as explained in (2) above) viz.,
<code>class B's doSomething()</code> is called.
The forward reference to instance variable <code>BVar</code>of <code>class B</code> is set to "0" and accordingly the display "BVar= 0"is made. This completes the constructor
execution of <code>class A</code>. Next in the
order of execution is the instance variable <code>BVar</code> of <code>class B </code> is set to "2222". After this the constructor of <code>class B</code> is called and we get the
display of "BBB". After this anather call to <code>doSomething()</code> is made so we get the display "BVar=2222".


Hope this clears your doubts..
Ravindra Mohan

[This message has been edited by Ravindra Mohan (edited July 23, 2001).]
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
That was great explanation.
Thanx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructors