• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about default constructor

 
dragon ji
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please explain to me which is right about the two options?and tell me the reason,thanx.
C. The default constructor invokes the no-parameter constructor of the superclass.
D. The default constructor initializes the instance variables declared in the class.
 
alberto ramos
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Every object belongs to a hierarchy of objects. The object from the Object class is the superclass of all objects. To construct an object from a class that does not define any constructors, a default constructor is created; and it is this constructor, the one in charge of calling the superclass.
When a new object is created initialization happens in this order:
1. static statements/blocks, IN THE ORDER they are defined.
2. instance initializer/blocks IN THE ORDER they are defined.
3. Finally, the constructor is called.
 
dragon ji
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank,alberto .
I am sorry but I can't confirm which is the right answer,please tell me.
 
alberto ramos
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Option D.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bievenido al Rancho de Java Alberto.
Welcome to javaranch.

The compiler will generate a default constructor for each class with no constructor declared by the programmer. It has no arguments, the same access specifier as the containing class.
The first instruction in its body is super(). The intention of this is to ensure the call to the proper chain of constructors to initialiaze the instance fields declared in superclasses before the ones declared in subclasses.
After "super();" the compiler places the code that initializes instances variables of this class, and the code within the instance initializers blocks written by the programmer. Both are placed in textual order, that is in the order in which they appear in the source code.
You can check this using "javap -c Myclass"
Thus both C and D are OK.
[ December 01, 2002: Message edited by: Jose Botella ]
 
Alfred Kemety
Ranch Hand
Posts: 279
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alberto, I think you missed on this one.. option C is the right answer. The implicit or explicit default constructor implicitly - if not explicitly - calls the default constructor of the direct super class and thus option C is right.
Default constructor doesn't initialize the instance variables, they're initialized to their default values even before any statement in the default or non-default constructor - other than the super() or this() calls - are executed.
Anyone correct me if I'm wrong....
 
alberto ramos
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After re-reading the question, I realize that D is wrong, because it is not the constructor that initializes the variables. So now I think the correct answer is C.
Gracias Jos´┐Ż.
Thank you Alfred.
 
dragon ji
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx,guys!
 
david eberhardt
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ji dragon:
please explain to me which is right about the two options?and tell me the reason,thanx.
C. The default constructor invokes the no-parameter constructor of the superclass.
D. The default constructor initializes the instance variables declared in the class.

my two cents worth:
I'd agree that C. is the only correct answer for sure (without any other details to go by)
example:

No instance variables appear in Dog or Object classes.
as for " D. The default constructor initializes the instance variables declared in the class."
I don't believe that the default no-arg contructor for Object initializes anything and I have not overrided it in Dog, so all that one can do is call super() implicitly.
I suppose I could unpack the code for the constructor in the Object class ???
[B]Am I correct?
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There a powerful tool to check this kinds of doubts. "javap -c Test"

You can see that the code that initializes the instance fields is placed by the compiler within the constructor.
 
david eberhardt
Ranch Hand
Posts: 158
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jose,
I missed the last line in one of your previous post which said " You can check this using "javap -c Myclass" "
Now it is very clear!
C and D - thanks!
[ December 02, 2002: Message edited by: david eberhardt ]
 
dragon ji
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx,Jose!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic