Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jtips Quiz4 No.1: About default constructor

 
yi miao
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:
public class Test {
double x, y;

Test() {
super();
}
}
The No arguments constructor Test() has "default" access modifier ( i.e with no access modifier ). True or False?
The Author's answer is False. He says: If the class is declared public, then the default constructor is implicitly given the access modifier public. If the class is declared protected, then the default constructor is implicitly given the access modifier protected and if the class is declared private, then the default constructor is implicitly given the access private.
I think the anwser for this one is True. I understand the default constructor is given only when the class contains no construction declaration, and then the constructor's modifier is implicitly given as class's modifier. In this question, a constructor is already given, and its access modifier is default meaning its accessibility is package level. So I think the anwser should be True. Am I right?
Besides, I wrote a subclass of Test class in another package. When I compile, I got error message: constructor Test() is not public in class pakgname.classname. I think if the Test() is public, subclass in another package should be able to access it.
Any coments for this question?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true that the default constructor is given the modifier of "public" if you don't code a default constructor. So I agree with you that the question is wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic