Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor Issue

 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From web page


The answer is given as c; why not a?
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The superclass may have a constructor other than a default constructor.

The following dummy code should make it clear:


Prints 5.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I don't agree with Aniket nor with the makers of this question.

a. is also correct.

They state:
a. The superclass must not have any constructor other than a default constructor.
should be wrong
But it is true.



this class has any (just one) constructor other than the default constructor, and so in the given scenario, the subclass would not compile.

if you put any constructor in a class, it simply does not have a default constructor.
The default constructor is only the "invisible" constructor, the compiler puts in, when you don't provide any.

The example that Aniket gave also has no default constructor, it has a parameterless constructor.

The makers of the test mixed up the terms "parameterless constructor" with "default constructor".

Language specification:
8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided: (...)



If choice a were
"The superclass must not have any constructor other than a parameterless constructor."
then it would be false.

Hopefully on the real exam you'll not find such wrong terminology.

Finally, there is one possibility when the code compiles, when the constructor you provide is the same as a default constructor would be - parameterless. But even then it should not be called "default constructor".



Yours,
Bu.
 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The makers of the test mixed up the terms "parameterless constructor" with "default constructor".


So did I!

Guess a 12 hour workday doesen't help in answering questions...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic