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
posted
0
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: 2912
posted
0
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
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
posted
0
Thanks April, That was a good example. It cleared that one out. Thanks again... - eric
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
posted
0
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
posted
0
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
posted
0
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
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
posted
0
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