File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a question about default constructor

 
chang nanny
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is most certainly 1 and 2. 3 is incorrect.
 
Davy Kelly
Ranch Hand
Posts: 384
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic