aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Doubt 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 "Constructor Doubt" Watch "Constructor Doubt" New topic
Author

Constructor Doubt

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
class top
{
public top() //line 3
{
System.out.println("top");
} //line 6
public top(String s)
{
System.out.println("BB");
}
}
public class bottom2 extends top
{
public bottom2(String s)
{
System.out.println("DD");
}
public static void main(String[] arg)
{
new bottom2("CC");
System.out.println("Invoked ");
}
}


In the above code sample
Output is >
top
DD
Invoked
************************************
Why "BB" is not getting printed???
***************************************
If i am right the program flow is
1:new button2("CC");
2:calls it's super class constructor > public top(String s)
3:then it class it's super class constructor......keeps on to Object class
4:then starts initialization
This way it happens.If my floe to understand this is wrong, please suggest me.
**********************************
My second question is if i comment from line 3 to line 6 ,it shows compilation error.According to me it shouldn't .As Class top and bottom2 have same argument constructor.


thanks
Deepak


Constant dripping hollows out a stone....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

2:calls it's super class constructor > public top(String s)


Why do you believe that a constructor will call the super constructor with the same signature? If this was true, then wouldn't the Object class need a constructor for every possible combination of constructors?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ajay Singh
Ranch Hand

Joined: Jan 04, 2008
Posts: 105
your second point is wrong,super makes an implicit call to the no argument constructor of the parent class instead of the argument constructor.If you comment those lines super cant find a no argument constructor of the parent class and hence the error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor Doubt