Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructors

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
pl see the code below :
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 ???
BBB
Bvar=2222
Avar=0
Can anybody tell me why the the doSomething() method invoked from within A's constructor is actually B's version of doSomething ?
 
Nirmala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got also the same answer ,but I m not clear on the flow of super class and subclass COnstructors,any deep explanation,please.
Thanks
Nirmala
 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Let me try to help u guys.
new B(); statement call the no argument constructor of class B. In the constructor of class B there is no call to "super()" the compiler put a default call to (super()) the construtor of class A.
In the constructor of class first it prints "AAA" then it call doSomething method. because we are overriding the doSomething() method of class A in class B. so B's version of doSomething() is called. so when it execute the doSomething method in class B. method try to print a instance variable of class B. here comes something importent. all the instance variables of any class are initialize after the execution of the construtor of parent class.
since method doSomething of class B is called from constructor of A so all the variables of class B has not been initialized. that is why it prints '0' afteter printing "AAA".
after returning fron constructor of class A first of all initialzer of class B take place and initialize all the instance variable. then continue execution in the constructor of class B.
first prints "BBB". then call doSomething method of class B and by now instance variable of class B has initialized so it prints "2222". then it prints the variable Avar( inherited from class A) which has default value to zero. so it prints '0'.
Hope i can express myself and help u guys. let me know if something is wrong.
vivek
[This message has been edited by Vivek Shrivastava (edited July 15, 2000).]
 
Nirmala
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much vivek and it's clarifies my doubt.
Thanks again.
Nirmala
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek ,If in the constructor of class A , the overridden method of class B is being called and instance variables of class B are initialized only after constructor of parent class.
Then why it is printing "Bvar=0" ,it should generate some error , as variable Bvar should not be known this time.
I am getting confused , could you pl. explain it more clearly....
thanx in advance ,
DS
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak,
This has to do with order of initialization of classes.
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variable declared in each superclass of the class. This happens even before the constructors are executed. The constructors are executed in the "parent-first" order, and in each case instance initializer(s) and instance variable initializer(s) for the class is executed.
In our example, when the constructor of A is being executed, though variables of B are not yet initialized, they have been allocated memory, initialized to their default value( zero for integral types ) and have been resolved. Hence it prints 0.
Ajith
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot, Ajith.
this is really a new concept for me.
thanks again,
DS
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic