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 package vs inheritance 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 "package vs inheritance" Watch "package vs inheritance" New topic

package vs inheritance

cata lin
Ranch Hand

Joined: May 11, 2000
Posts: 44
Congratulations maha anna for the exam.
I played a little the roundup game. At question marked C9 #209 I was asked :
For object X to access a method of object Y, when the method has no access modifier, , object X MUST be an instance of class which is
a: in the same package as Y
b: declared public
c: a subclass of Y
d: the same class as Y
The answer was indicated as a, while I choosed c.
Why am I wrong , please ?
Many thanks.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
If a class/method has no access modifier, it is said to have "package" access. ie., only accessible from the classes/methods in the same package. The default modifier is more restrictive than "protected" which provides subclass access.
Hope this helps. If not, try to write a small program by yourself and verify my statements.
Good luck.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
A small correction in your statement.
'The default modifier is more restrictive than "protected" which provides subclass access which are in other packages too.. The other packages is VERY important. because ONLY subclasses in other packages get this previlege NOT non-subclasses in other packages.
cata lin,
This is the concept:
package (no access modifier) - means that class/that method/that variable is seen all the fellow classes in the SAME PACAKGE ONLY. And also if and only if a class is in the SAME pacakge. The fellow class may be anything. It may be a subclass/may not be a subclass. All in the same pack can use this no-access-modifier class/ no-access-modifier method/
no-access-modifier var.
Also note this. Even if is a subclass , if it fails to be inside the SAME package , which means ,if it is in another package, then the subclass is treated as an outsider.
It is like saying, eventhough you are a member of my family (via class inheritance), since you are NOT frindlier to us (not in the same pack , then you are NOT invited here Being a good friend is MORE IMPORTANT than just being a child of the familiy alone like that.
So coming back specific to your doubt, Since the qstn specifically says the method has a default access, in order to get the previlege of using this method, the other class of object X HAS TO BE IN THE SAME PACAKGE. , If not you are treated as an alien. Clear ?
maha anna
class Xclass {
void xMethod() {
Yclass yObject = new Yclass();
class Yclass {
void yMethod() {}

[This message has been edited by maha anna (edited May 11, 2000).]
cata lin
Ranch Hand

Joined: May 11, 2000
Posts: 44
thank you.
Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
Thank you for the clarification. I somehow always miss that sugaring on the cake
wood burning stoves
subject: package vs inheritance
Similar Threads
Doubt in the following code
doubt in == and .equals()
accessing protected members
SCJP prep - protected method inheritence question