This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Acceess Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Acceess Modifiers" Watch "Acceess Modifiers" New topic
Author

Acceess Modifiers

Arpana Rai
Ranch Hand

Joined: Nov 12, 2002
Posts: 93

In the above Q i thott the right answers are 1 and 2 only.But the given right answers include 3 also.I tried to find out the reason for it and what i concluded is given below.Plz some1 tell me wether i am right or wrong.

It is b'coz the no-args constructor(which is also a default construtor) has default access modifier and it's subclass in different package may call this no-args constructor implicitly which is not allowed coz of it restrictice access.Had this been some construct with argument then it cud have been extended even outside the package coz of pulic modifier of class.


regds
Arpana
[ November 25, 2002: Message edited by: Arpana Rai ]

SCJP1.4(91%)
Keen Chen
Ranch Hand

Joined: Nov 12, 2002
Posts: 47
i think so also.
but, why not try to compile it and let jvm tell u why?


SCJP 1.4 100% @ Peking, China <br />~~~~~~~~~~~~~~~~~~~~~<br />但使龙城飞将在, 不教胡马度阴山!
Keen Chen
Ranch Hand

Joined: Nov 12, 2002
Posts: 47
and,
when u extent a from a diff package,
how can u import or refer a class which is in default package?
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
It is b'coz the no-args constructor(which is also a default construtor) has default access modifier and it's subclass in different package may call this no-args constructor implicitly which is not allowed coz of it restrictice access.Had this been some construct with argument then it cud have been extended even outside the package coz of pulic modifier of class.

Arpana, I cudn't understand what u mean to say by "Had this been some construct with argument then it cud have been extended even outside the package coz of pulic modifier of class.".
The conclusion I could come out after executing ur program and reading through JLS are :
1)An implicit default constructor has the same access modifier as that of the class. As per JLS "If the class is declared public, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier. "
In class A if constructor was not defines at all, it would have been public.
JLS- 8.8.7 Default Constructor
2)An explicit constructor (including the no arg constructor) gets the access according to the access modifier specified in the constructor. If no access modifier is specified as in class A, then the constructor will have default access that is access only in the current package.
3)The option 3 "The class A cannot be extended outside the package in which it is defined. "
in the question is right because
The explicit no arg constructor has default access, and it's subclass in different package may call this no-args constructor implicitly which is not allowed coz of it restrictice access.


--Shweta<br />SCJP 1.4 <br />SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Acceess Modifiers
 
Similar Threads
class access
public
constructor
Access modifiers question
reference of class!