File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Implicitly given access modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Implicitly given access modifier" Watch "Implicitly given access modifier" New topic
Author

Implicitly given access modifier

Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
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
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi,
I think, if you donot put a access modifier for a constructor, it implicitly means public!
-- Sandeep


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3313
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
April.Johnson
Ranch Hand

Joined: May 02, 2001
Posts: 48
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

Joined: Jul 09, 2001
Posts: 74
Thanks April,
That was a good example.
It cleared that one out.
Thanks again...
- eric
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
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
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
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


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
Thanks Jane, I appreciate that.
--Farooq
Jo Lee
Ranch Hand

Joined: Jun 26, 2001
Posts: 35
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
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implicitly given access modifier