aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Default access modifier. What does it do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Default access modifier. What does it do?" Watch "Default access modifier. What does it do?" New topic
Author

Default access modifier. What does it do?

Raef Kandeel
Ranch Hand

Joined: Aug 05, 2007
Posts: 87
What I read in books say that if there is no access modifier then the default, sometimes called package-private modifier is used. My experiments prove that if there is no access modifier, then subclasses can access the variable but packages can�t which is opposite of what is expected. Consider the following code:-
Experimentation.java
public class Experimentation {
static void main someMethod() {}
}
class Extension extends Experimentation {
public void callingMehod () {
someMethod();
}
}

This code compiles fine!!! Now, Consider this next code please:-
Experimentation.java
package com.Experimentation,

public class Experimentation {
static void someMethod () {}}

Extension.java
import com.Experimentation.Experimentation;

class Extension {
public void CallinMethod () {
(new Experimentation()).someMethod();
}
}

This code gives the following error:-
C:\Sun\AppServer\jdk\bin\Extension.java:5: someMethod() is not public in com.Experimentation.Experimentation; cannot be accessed from outside package
(new Experimentation()).someMethod();
^
1 error

Which shows that in opposite to what books say, if there is no access-modifier, the method is available to the subclasses not the package.


SCJP, SCWCD
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
A couple of fine points I'll throw in....

1) to place a class in a package you use the package statement
(package Experimentation.java

2) to use a class from a package use the import statement
(import Experimentation.java

I bring these up because your souce looks as though you just put a line that says

Experimentation.java

as if that were going to put your class in that package.

Next,

3) there is one package statment allowed in a source file. That means that any classes you type in will all go to the same package as specified in the package statement.

That means that it will take at least two separate source files to test what you are wanting to test.

One will start with

package Experimentation.java;

// code to declare your first class

The other, which will test for access.. will start with

package Extension.java;

import package.java;

// code to declare your second class that tests for access.



NOW you will have two classes in tow different packages....


------------------------
Bob
SCJP - 86% - June 11, 2009
Raef Kandeel
Ranch Hand

Joined: Aug 05, 2007
Posts: 87
Dear Bob Ruth,
That is what I did. Experimentation.java and Extension.java are not in the source code. They are the filenames. Thanks for letting me clear that up.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Oh MAN did I ever misread/misinterpret THAT.

Sorry...
Murali Kakarla
Ranch Hand

Joined: Jul 11, 2007
Posts: 80
Hi Raef

In your first example for both the classes package name is not mentioned. Hence both reside in 'default' package and hence can access default members.

In your second example, Experimentation class is in com.Experimentation package whereas Extension class is in 'default' package as no package name is not mentioned and hence the compilation error.

I hope this clears your question.

Murali...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default access modifier. What does it do?