• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implicitly given access modifier

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I ran into this question on www.jtips.net Quiz#4.1
Question:
Given:

The No arguments constructor Test() has "default" access modifier ( i.e with no access modifier ). True or False?
the jtips.net's answer is:
False. 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've tried a few examples and run javap -c on 'em, and didn't see any of the "supposedly-implicitly-given" access modifier as noted by jtips.net.
Could anyone please enlightened me?
Thanks.
- eric
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think, if you donot put a access modifier for a constructor, it implicitly means public!
-- Sandeep
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given answer is wrong. However the statement about "implicit" contstructor added by the compiler is correct. The problem here is that this constructor is added by the programmer and not by compiler. And it has no access modifier (ie. it has default accessibility). The compiler does not mess with the constructor provided by the developer. It adds the contsctuctor only if no constructor is provided by the developer.
HTH,
Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Desai Sandeep:
I think, if you donot put a access modifier for a constructor, it implicitly means public!


Incorrect. If you don't specify an access modifier the constructor takes on the "default" access or "package" access like any other method or instance variable and is accessible only from other classes in the the same package.
Try the code yourself:

and

Obviously, place them in different directories. This won't compile unless you specifically tell DefaultConstructor() to be public.
April
 
Eric Pramono
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks April,
That was a good example.
It cleared that one out.
Thanks again...
- eric
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We recently had this same discussion. Try:
http://www.javaranch.com/ubb/Forum24/HTML/010960.html
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is correct;The access modifier is accepted by the compiler on "as-is" basis.Thank you, April and Thomas for clearing on this.
-- Sandeep
[This message has been edited by Desai Sandeep (edited July 28, 2001).]
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just got a note from Suresh over at JTips and he says that they will be correcting the error in their test.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this wonderful discussion. Here I understand that the compiler honours the access modifier of the constructor, but what about if their is no constructor and compiler provide a default constructor, what would be the access modifier (accessibility) of that constructor,
--Farooq
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Muhammad,
If you do not include a constructor, the compiler will automatically create a default no-arg constructor with the access modifier used to declare the class. i.e. if the class is declared to be 'public' the compiler will create a 'public' no-arg ctor. If the class has default access, the ctor will have default access, etc.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane, I appreciate that.
--Farooq
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about if you add a constructor with parameter list? would that follow the same rule as the one with empty parameter list? i.e. the compiler honor the accessmodifier of the class if you do not specify an access modifier for the constructor?
Thanks
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jo Lee:
What about if you add a constructor with parameter list? would that follow the same rule as the one with empty parameter list? i.e. the compiler honor the accessmodifier of the class if you do not specify an access modifier for the constructor?
Thanks


If you do not provide an access modifier for a constructor, it uses the default access modifier. That holds true whether the constructor has 0 or more parameters. Only when a constructor is not provided by the author (when the compiler generates a no-args constructor for you) does the special rule about using the class access modifier hold.
I hope that helps,
Corey
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic