Controlling Access to Members of a Class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Controlling Access to Members of a Class

Hybrid View

  1. #1
    Joe Guest

    Controlling Access to Members of a Class


    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:

    --------------
    1) Alpha.java
    -------------
    package Greek;

    public class Alpha {
    protected int iamprotected;
    protected void protectedMethod() {
    System.out.println("protectedMethod");
    }
    }

    --------------
    2) Gamma.java
    --------------
    package Greek;

    class Gamma {
    void accessMethod() {
    Alpha a = new Alpha();
    a.iamprotected = 10; // legal
    a.protectedMethod(); // legal
    }
    }

    --------------
    3) Delta.java
    --------------
    package Latin;

    import Greek.*;

    class Delta extends Alpha {
    void accessMethod(Alpha a, Delta d) {
    a.iamprotected = 10; // illegal
    d.iamprotected = 10; // legal
    a.protectedMethod(); // illegal
    d.protectedMethod(); // legal
    }
    }

    -----------------------------------------------------------------
    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

    C:\test\Greek>javac Gamma.java
    Gamma.java:5: cannot resolve symbol
    symbol : class Alpha
    location: class Greek.Gamma
    Alpha a = new Alpha();
    ^
    Gamma.java:5: cannot resolve symbol
    symbol : class Alpha
    location: class Greek.Gamma
    Alpha a = new Alpha();
    ^
    2 errors

    ----------------------------------------------------
    And when I try to compile Delta.java I get 5 errors

    C:\test\Latin>javac Delta.java
    Delta.java:3: package Greek does not exist
    import Greek.*;
    ^
    Delta.java:5: cannot resolve symbol
    symbol : class Alpha
    location: class Latin.Delta
    class Delta extends Alpha {
    ^
    Delta.java:6: cannot resolve symbol
    symbol : class Alpha
    location: class Latin.Delta
    void accessMethod(Alpha a, Delta d) {
    ^
    Delta.java:8: cannot resolve symbol
    symbol : variable iamprotected
    location: class Latin.Delta
    d.iamprotected = 10; // legal
    ^
    Delta.java:10: cannot resolve symbol
    symbol : method protectedMethod ()
    location: class Latin.Delta
    d.protectedMethod(); // legal
    ^
    5 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

  2. #2
    Colin McGuigan Guest

    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




  3. #3
    Colin McGuigan Guest

    Re: Controlling Access to Members of a Class

    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.

    --
    Colin McGuigan



  4. #4
    Guest

    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
    >


    Thanks again Colin!

    Alpha.java and Gamma.java compile OK. When I tried to compile Delta.java,
    I got the following 2 errros:
    ----------------------------------------------------------------------
    C:\test\Latin>javac -classpath c:\test delta.java
    delta.java:7: iamprotected has protected access in Greek.Alpha
    a.iamprotected = 10; // illegal
    ^
    delta.java:9: protectedMethod() has protected access in Greek.Alpha
    a.protectedMethod(); // illegal
    ^
    2 errors
    ----------------------------------------------------------------------
    I think this is correct? Right?
    Joe

  5. #5
    Colin McGuigan Guest

    Re: Controlling Access to Members of a Class

    java.@127.0.0.1 wrote:
    > Alpha.java and Gamma.java compile OK. When I tried to compile
    > Delta.java, I got the following 2 errros:
    > ----------------------------------------------------------------------
    > C:\test\Latin>javac -classpath c:\test delta.java
    > delta.java:7: iamprotected has protected access in Greek.Alpha
    > a.iamprotected = 10; // illegal
    > ^
    > delta.java:9: protectedMethod() has protected access in Greek.Alpha
    > a.protectedMethod(); // illegal
    > ^
    > 2 errors
    > ----------------------------------------------------------------------
    > 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



  6. #6
    Joe Guest

    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
    >
    >


    Thanks again Colin for your help in explaining this example!
    Joe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center