This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
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: 12761
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
Similar Threads
cannot resolve symbol
OutPut of abstract class constructor calling method
simple one??
polymorphic method in abstract constructor
Constructors and Polymorphism