Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why not give a no-arg constructor

 
Wang Guo hui
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wang Guo hui
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic