In the Sun Microsystems Java Tutorial
Trail: Learning the Java Language
Lesson: Classes and Inheritance
Title: Controlling Access to Members of a Class
URL:
java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

I'm having problems compiling and testing 3 simple class files from the Protected
section. The 3 java classes are below:

-----------------------------------------------------------------
The directory structure I setup for the 3 java class files is below:

C:\test\Greek> --> This is where I placed both the Alpha.java
and Gamma.java files, as they are both in
the Greek package.

C:\test\Latin> --> This is where I placed the Delta.java file
which extends the Alpha class,
but is in the Latin package
and has the import Greek.*; statement.
-----------------------------------------------------------------
I'm using Java(TM) 2 Compiler and Runtime Environment,
Standard Edition (build 1.4.1-b21) which is installed
in C:\j2sdk1.4.1\...

The PATH env variable includes C:\j2sdk1.4.1\bin.

-------------------------------------------------
I can compile Alpha.java OK

C:\test\Greek>javac Alpha.java

C:\test\Greek>
-----------------------------------------------------
But when I try to compile Gamma.java, I get 2 errors

-----------------------------------------------------------------
PLEASE HELP! How would I compile and run this example? I'm thinking that
if I have the java files in sub-directories from C:\test> that I should be
able to compile them all from C:\test with the proper CLASSPATH. What would
that be?
Also, I'm not sure if I need to change any of the package or import statements
in any of the 3 java program files?

Thank you very much for your help,
Joe

10-17-2002, 02:14 PM

Colin McGuigan

Re: Controlling Access to Members of a Class

Joe wrote:
> PLEASE HELP! How would I compile and run this example? I'm thinking
> that if I have the java files in sub-directories from C:\test> that I
> should be able to compile them all from C:\test with the proper
> CLASSPATH. What would that be?
> Also, I'm not sure if I need to change any of the package or import
> statements in any of the 3 java program files?

In this case, you'd want the classpath to include 'C:\test'. You could
do this either through the CLASSPATH environment variable, or by calling
javac with the -classpath parameter (which is the recommended way; it
can also be abbreviated to -cp), as so:

c:\test\greek> javac -classpath c:\test alpha.java

c:\test\greek> javac -classpath c:\test gamma.java

c:\test\latin> javac -classpath c:\test delta.java

The class path, plus the name of the package (with slashes replacing
periods) is searched for the classes, so Delta's usage of Greek.Alpha
would cause the compiler to look for an Alpha.class in c:\test\greek\

--
Colin McGuigan

10-17-2002, 02:30 PM

Re: Controlling Access to Members of a Class

"Colin McGuigan" <cmcguigan@imany.com> wrote:
>In this case, you'd want the classpath to include 'C:\test'. You could
>do this either through the CLASSPATH environment variable, or by calling
>javac with the -classpath parameter (which is the recommended way; it
>can also be abbreviated to -cp), as so:
>
>c:\test\greek> javac -classpath c:\test alpha.java
>
>c:\test\greek> javac -classpath c:\test gamma.java
>
>c:\test\latin> javac -classpath c:\test delta.java
>
>The class path, plus the name of the package (with slashes replacing
>periods) is searched for the classes, so Delta's usage of Greek.Alpha
>would cause the compiler to look for an Alpha.class in c:\test\greek\
>
>--
>Colin McGuigan
>

Colin McGuigan wrote:
> The class path, plus the name of the package (with slashes replacing
> periods) is searched for the classes, so Delta's usage of Greek.Alpha
> would cause the compiler to look for an Alpha.class in c:\test\greek\

Slight mistake: the directory names (like the .java names) have to be
case sensitive, so if you have 'package Greek;' and 'import Greek.*;'
then the directory name must be c:\test\Greek\ -- c:\test\greek\ (lower
case 'g') will not work.

Well, it's correct in that you should get those errors -- the code is
not compiilable. That's what the tutorial is showing you: 'protected'
properties/methods can only be accessed by...

1) classes in the same package (Gamma can access Alpha's protected
properties/methods)

2) subclasses (Delta can access its own protected properties/methods
inherited from Alpha, but cannot access them on other instances of
Alpha)

--
Colin McGuigan

10-17-2002, 02:58 PM

Joe

Re: Controlling Access to Members of a Class

----------------------------------------------------------------------
>> I think this is correct? Right?
>
>Well, it's correct in that you should get those errors -- the code is
>not compiilable. That's what the tutorial is showing you: 'protected'
>properties/methods can only be accessed by...
>
>1) classes in the same package (Gamma can access Alpha's protected
>properties/methods)
>
>2) subclasses (Delta can access its own protected properties/methods
>inherited from Alpha, but cannot access them on other instances of
>Alpha)
>
>--
>Colin McGuigan
>
>