aspose file tools*
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class" Watch "abstract class" New topic
Author

abstract class

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 22, 2000
Posts: 12676
    
    5
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

Java Resources at www.wbrogden.com
Anonymous
Ranch Hand

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

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


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

Joined: Jul 13, 2000
Posts: 29
Hi,
The final line of this output would be
" Sub class constructor 7".


hyby2000@yahoo.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract class
 
Similar Threads
cannot resolve symbol
Constructors and Polymorphism
simple one??
polymorphic method in abstract constructor
OutPut of abstract class constructor calling method