It's not a secret anymore!
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor Doubt" Watch "Constructor Doubt" New topic

Constructor Doubt

Deepak Bobal
Ranch Hand

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

In the above code sample
Output is >
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.


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

Joined: Sep 28, 2004
Posts: 20163

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?


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:
subject: Constructor Doubt
It's not a secret anymore!