• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abstract class abs
{
abstract void draw();
abs()
{ System.out.println("before draw");
draw();
System.out.println("after draw");
}
}
class sub extends abs
{
int r=1;
sub(int r)
{ this.r=r;
System.out.println("sub class constructor:"+r);
}
void draw()
{System.out.println("draw in sub:"+r);}
public static void main(String a[])
{ new sub(7);}
}
The o/P: before draw
draw in sub: 0
after draw
subclass constructor.
In the above o/p, I am not able to understand ,why the value of r is printed as 0 even when initialised to 1.
ThankYou.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats pretty tricky - I think it has to do with the order in which things are done in constructors. I think it is:
1. Memory is reserved and initialized to zero
2. parent constructors all the way back to Object are called
3. the remainder of the sub() constructor is executed.
In step 2, the value it finds is initialized memory.
Bill
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ! But, Is that the correct order of initialising
and assigning.
ThankYou
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AVN,
See a related discussion here
Ajith
 
Helen Yu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The final line of this output would be
" Sub class constructor 7".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic