Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Jtips Quiz4 No.1: About default constructor  RSS feed

 
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
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.
 
Always look on the bright side of life. At least this ad is really tiny:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!