aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a question about default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a question about default constructor" Watch "a question about default constructor" New topic
Author

a question about default constructor

fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
hi,everyone
I encouter following question :
Which two statements are true regarding the creation of a default constructor? (Choose Two)
A. The default constructor initializes method variables.
B. The default constructor invokes the no-parameter constructor of the superclass.
C. The default constructor initializes the instance variables declared in the class.
D. If a class lacks a no-parameter constructor, but has other constructors, the compiler creates a default constructor.
E. The compiler creates a default constructor only when there are no other constructors for the class.
I think the B and E are right answer, but it is said that C and E are correct,Is the C right?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I would not select C as a correct answer. The default constructor may initialize instance member variables, but it is not required to do so. Therefore, this statement is not always true and, therefore, should not be selected as a correct answer.
B, however, is a correct answer. The default constructor (which is automatically generated) will always contain an invocation of the default constructor of the parent class.


SCJP Tipline, etc.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

The default constructor may initialize instance member variables, but it is not required to do so.

It doesn't . The instance variables are initialized (to their default values) before the constructor is called.


Groovy
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
after I post upper question,I check the book writen by Sierra & Bates,
I found the follow in page 314;
It is a simple example of a constructor of no_argu;
Horse h=new Horse();//Horse is subclass of Animal
the main idea is ,when Horse constructor is invoked,It invoke Object constructor,Animal constructor in turn,before every constructor complete,
the class's instance variables are given their explicit values(if any),this means the answer C is right,
the answer B's wrong may be here:
" invokes the no-parameter constructor ", the no-parameter constructor is not necessary the default constructor,so B is wrong.
Is it correct that I understand like this ?
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
sorry,my explaintion about answer B is not right,I think its don't matter that the "constructor no_argument" used here,
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Hello folks,
My opinion is that answer C is correct (here I'm not going to discuss answer E ) and I will try to explain why.
Ok, everyone knows that default constructer is class_name(), and it can be implemented either by developer or by compiler, if it is implemented by compiler then if there is a sub-classing the first implicit statement after class_name() will be super() but if default constructor was implemented by developer then no one can guarantee that super() will be called because super-class can have another form of constructor super(int I) , super(String _name) etc. therefore one of them could be called.

( here is a small example )
public class A {
String name;
public A(){
this("Default");
}
public A(String _name){
name = _name;
}
}
public class B extends A {

public B(){
super(�Hello�); // and not super()
}
}
On another side what is for sure is that any constructor initialize instance variables, therefore answer C is correct.

If I misunderstood please correct me.


Aleksandar Stojanovic
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hello Aleksandar.
no one can guarantee that super() will be called because super-class can have another form of constructor super(int I) , super(String _name) etc.


First, your class A implicitly extends from java.lang.Object (since you did not explicitly specify a super class). In other words java.lang.Object is your class A's superclass.
Second, in class B constructor, a call is made to A constructor that accepts String. Since you did not specify super() as the first statement in that constructor, the compiler will implicitly place one for you so that the java.lang.Object constructor is called.


SCJP 1.4<br />SCWCD 1.4
Aleksandar Stojanovic
Greenhorn

Joined: Sep 08, 2003
Posts: 26
Hello Deniz,
I appreciate that you paid attention on my post but if you check my example with any debug you will see that the constructor of class Object is only called from public A(String _name) as first statement, that is implicitly added by compiler. And there is not any direct call to class Object from B. That is based on constructor-chaining.
public B() {
Super(�Hello�); --> public A(String _name) {
//implicit super() --> Object()
name = _name;
}
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Yep. That's exactly what I meant (probably not properly worded in my previous post). So in your example super() is guaranteed called.
then no one can guarantee that super() will be called
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
Hello Aleksandar.
In your post
>>quot--------------------------------------------------
...but if default constructor was implemented by developer then no one can guarantee that super() will be called because super-class can have another form of constructor super(int I) , super(String _name) etc. therefore one of them could be called.
>>------------------------------------------------------
but in the book of K&B (page 315,317),the follow is provid:
...A no-arg constructor is not necessary the default constructor...
...the default constructor is the one the compiler provides!...
...the default constructor includes a no-arg call to the super constructor...
so,I think that in your explain "The defalut constructor" and "the no_arg constructor " are mixed,and the answer B should be correct.
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Fei. I think you are right. B and E are the right answer and I would not agree on C. Just curious, where did you come across this question? Is it in one of the mock exams?
H Gokulam
Ranch Hand

Joined: Nov 04, 2003
Posts: 46
Hello Aleksandar,
A no-argument constructor is not necessarily the default constructor, although the default constructor is always a no-arg constructor. (Page 315 of K&B).
So a default constructor is not the one which is implemented by the developer. That is just a no-arg constructor.

So answer B is perfectly alright.

Hi fei peng : Please check page 317 of K&B. Table 5-4.
Regards,
Hari.


Harikumar G<br />SCJP 1.4
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
Hello everyone,
This question is come from a post in a Java forum named "csdn",picked up from a set of exam questions name "147",NO.125.after last discuss,I found this set exam,and check the answer,It is B and E,though I make a mistake,I think we don't waste the time.
and the answer C is confusing,pradeep says that the instance variable is initilazed before the constructor is invoked,not in constructor,is this correct?
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Fei. I think C is incorrect. The question being asked is about default constructor:
"Which two statements are true regarding the creation of a default constructor"
Default constructors have nothing to do with initialization of variables. Variables have their default values (int=0, boolean=false, String=null, etc) and are initialized before any constructor (including the ones up the inheritance hierarchy) completes. Try debug/step-through the code below in your IDE.
My code below doesnt have any default/no-arg constructor and yet my instance variable is initialized to its default value.


However, you can use a constructor to explicitly initialize your variables.
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
thanks.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by dennis zined:
Default constructors have nothing to do with initialization of variables.


This is incorrect. Constructors, be it default or not, do initialize instance variables depending on how it is constructed.
Consider the followign code and its corresponding bytcodes:

As you can see in the bytecodes, the default constructor Sample() initialized the variable j at lines 5 & 7.
[ November 28, 2003: Message edited by: Alton Hernandez ]
dennis zined
Ranch Hand

Joined: Mar 07, 2003
Posts: 330
Hi Alton. Learned something new today...reading byte codes and what is actually going on beneath java programs. thanks.
Based on your post, does this mean then that the answer "C" to Fei's question is correct? Pls. see top part of this page.
fei peng
Ranch Hand

Joined: Sep 15, 2003
Posts: 40
Hi Alton.
In an example of the book by K&B,the default constructor code just is like following,
Foo(){
super()
}
There is no any assign statements,what you post means that the compiler insert assignment statements automatily,is it?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about default constructor