This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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

chang nanny
Greenhorn

Joined: Feb 15, 2004
Posts: 14
Hello,
Which two statement are true?
1.the compiler creates a default constructor only when there are no other constructor for the class.
2.The default constructor invokes the no-parameter constructor of the superclass.
3.The default constructor initializes the instance variables declared in the class.
Thank you.
sai kumar
Ranch Hand

Joined: Mar 31, 2004
Posts: 72
Hi ,
Originally posted by chang nanny:
Hello,
Which two statement are true?
1.the compiler creates a default constructor only when there are no other constructor for the class.
2.The default constructor invokes the no-parameter constructor of the superclass.
3.The default constructor initializes the instance variables declared in the class.
Thank you.

I would say 1. and 3. will form the answer. As for 2., if the default constructor calls another constructor using this(..) then it would not call the no-param constructor of the super class.
Thx,
bye
sk
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
The answer is 1 and 2.
Check out the following code to show that the default constructor will call the default contructor will call the default constructor of the parent class:

Compile and run class Child and you will get the following output on the console:
Base Constructor

When an object of class Child is instantiated, class Child's default constructor calls the default constructor of it's parent class Base.
Answer 3 is false. For instance, the class does not have a default no-argument constructor if you provide one constructor that takes an argument...
ie...

In this case, the class will NOT have a default constructor, therefore, the default constructor cannot be called to initialize instance variables.
Mark


Mark Patrick<br />SCJP 1.4
Mark Patrick
Ranch Hand

Joined: Feb 22, 2004
Posts: 51
The code from the second example should be

...forgot to include a string as an argument when Child was instantiated. The previous code would have caused a compile time error.
Whoops...
Mark
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The answer is most certainly 1 and 2. 3 is incorrect.


SCJP Tipline, etc.
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
I would say 1 & 2 are correct because you don't have to initialise the variables in the constructor. you can initialise it when it is declared.
Davy


How simple does it have to be???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a question about default constructor