File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class 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 "abstract class" Watch "abstract class" New topic

abstract class

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
abstract class abs
abstract void draw();
{ System.out.println("before 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.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
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.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks ! But, Is that the correct order of initialising
and assigning.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
See a related discussion here

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Helen Yu

Joined: Jul 13, 2000
Posts: 29
The final line of this output would be
" Sub class constructor 7".
I agree. Here's the link:
subject: abstract class
It's not a secret anymore!