|
-
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks