• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

a question about default constructor  RSS feed

 
Greenhorn
Posts: 14
  • 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.
 
Ranch Hand
Posts: 72
  • 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
 
Ranch Hand
Posts: 51
  • 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
  • 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
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is most certainly 1 and 2. 3 is incorrect.
 
Ranch Hand
Posts: 384
  • 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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!