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 why not give a no-arg constructor 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 "why not give a no-arg constructor" Watch "why not give a no-arg constructor" New topic

why not give a no-arg constructor

Wang Guo hui

Joined: Oct 17, 2004
Posts: 2
I have finished the Chapter 5 of SUN SCERTIFIED PROGRAMMER&DEVELOPER FOR JAVA2.I found that if you want your class can be extended,you must give it a no-arg constructor,if not,you can't extend it! But if you overload the constructor,the default constructor(you know, the no-arg one) will not be created.Of course we will do that,because the no-arg constuuctor can do nothing!!and so we hope our class can be extended,we need that! Just a final class don't need to be extended.
So, the question is : why not give eath class a no-arg constructor by default,regardless it had been overloaded???
In book :

Table5-4 in page 318, the third&fourth, if you whrite that:
1 class Be extends Foo {
2 Be() {
3 }
4 }
will not work!!! Because "super();" will be added to line3,and Foo class don't have that!

------------------------------<br />Hi , I'm a Chinese. Mybe some truble, please help if you found.Thank you!<br />--------------------------<br />Java & Linux
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
So, the question is: why not give each class a no-arg constructor by default, regardless of whether it has been overloaded???

As an example, suppose you have a class Password:

If the compiler were to generate a default constructor, then a Password object could be created with a null for strPassword.

Sometimes a class designer wants to enforce some needed criteria for object creation. The "do nothing" default constructor is generated as a convenience.
rimzim sinha

Joined: Oct 26, 2004
Posts: 17
Hope this helps!!
The compiler creates the default constructor if you have not provided any other constructor in the class. It does not take any arguments. The default constructor calls the no-argument constructor of the superclass. It has the same access modifier as the class.

However, the compiler will not provide the default constructor if you have written at least one constructor in the class

. Constructor can be overloaded, but they are not inherited by subclasses.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
1. The default, no-arg constructor is ONLY provided if you do not provide a constructor.
2. If you provide a constructor but also wish to have a no-arg constructor, you MUST define it yourself.

3. When a subclass is instantiated, the superclass of that class must be instantiated prior to the class being instantiated. This is done by invoking the superclass' constructor from the subclass constructor. This happens every time. If you do not explicitly invoke the superclass constructor, the compiler does it for you. Here's an example (untested):

As you can see, the Parent class constructor was invoked implicitly. As we did not explicitly invoke the superclass' constructor in the subclass' constructor, the compiler automatically inserted an invocation. Which superclass constructor does it invoke implicitly? The no-arg one! That's why, if you don't have a no-arg constructor in your superclass, you MUST invoke the proper superclass constructor explicity from the subclass constructor. Here's another example:

As you can see, I successfully extended Parent without having a default constructor. In such a case, all subclasses are required to explicity invoke the proper superclass constructor because the implicit one that the compiler inserts (the no-arg one) doesn't exist.

Check out this section of the JLS: §8.8.5 Constructor Body

SCJP Tipline, etc.
Wang Guo hui

Joined: Oct 17, 2004
Posts: 2
Thanks for Corey McGlone ,rimzim sinha & J Borderi,i think that i get it with your help. I want to say sth,but i can't say it in English

Give me sometime,maybe i can translate it.The following is Chinese:

I agree. Here's the link:
subject: why not give a no-arg constructor
It's not a secret anymore!