my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access modifiers" Watch "Access modifiers" New topic
Author

Access modifiers

Marcelo Vieira
Greenhorn

Joined: Jul 13, 2010
Posts: 4
Hello everyone,

I just started studying java and I found one thing that is making me a bit confused. Please, consider the following code:




My intention with the above code was to understand how does the default access to a class works (and a class member). As I could understand, once the classes are in the same package, it is possible to use the class C (in the file A.java) in the code of class B (in the file B.java). However, I'm getting the following error after trying to compile B:

C:<my path>\src>javac -d ../bin test/B.java
test\B.java:5: cannot find symbol
symbol: class C
public class B extends C{
^
1 error

The directory structure is as follows:

/root
--------/src
-------------/test
------------------A.java
------------------B.java
------- /bin
------------/test
-----------------A.class


Does anybody have any idea about why I can't access the C in the class B, even both belonging to the same package?

Thank you very much.
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
The default class modifier allows a class to be used (i.e. new C()) from any other class within the same package. It does not, however, allow classes within the same package to extend the class. You must set the class modifier to protected or public to do that.
Marcelo Vieira
Greenhorn

Joined: Jul 13, 2010
Posts: 4
Tom Reilly wrote:The default class modifier allows a class to be used (i.e. new C()) from any other class within the same package. It does not, however, allow classes within the same package to extend the class. You must set the class modifier to protected or public to do that.


Hello Tom, thanks for the quickly response.

If I'm not wrong, a class cannot have a protected modifier. I've tried to instantiate a variable of type C instead of extending the C class (the code is shown below), but I still get the same error. The compiler still can't find the class C.



I've tried, just for curiosity, to extend and instantiate the A class (wich is public) and it worked with no problems, as we would expect.

Any ideas?

Thank you
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Why isn't there any C.class in your bin directory ? Maybe you forgot to recompile A.java ?


[My Blog]
All roads lead to JavaRanch
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
My bad. I actually didn't compile it myself. and I looked at the table in wikipedia for modifiers (not class modifers). Anyway, I compiled your code (original and updated) and got no errors. You say you compiled with:I don't use the command line javac much. Do you need to set your classpath?

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Marcelo Vieira wrote:
Does anybody have any idea about why I can't access the C in the class B, even both belonging to the same package?


If you are in the test directory, then compile your A.java file first, and make sure there will be two class files. A.class, and C.class. Then compile B.java, everything will be fine!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Tom Reilly wrote: . . . Do you need to set your classpath?
I don't think so.
Marcelo Vieira
Greenhorn

Joined: Jul 13, 2010
Posts: 4
Thank you all for the help, but it doesn't compile. I've excluded the directory structure so that now I don't have any packages, ie., both files (A.java and B.java) are in my root directory, without any "src" or "bin" directories, and I also don't have any constraints about the class path, onde the root directory in my system path. However, even this structure didn't let me compile the B.java file. I'm still getting the same error: the compiler could not find the C class.

I'l try to figure out what is happening and when I get it I'll post here, for anyone interested.

Regards.
Marcelo Vieira
Greenhorn

Joined: Jul 13, 2010
Posts: 4
Well, I "kind of" figured out why the class B was not compiled. While studying I've created and compiled many A.java files (of course, in the same path). For some reason, the A.class file wasn't being updated whenever I compiled it. I just had to exclude the .class file and I became able to compile it again. But the most important thing is that I could see how does the default access works!

Regards
 
 
subject: Access modifiers
 
Similar Threads
CLASSPATHS; please critique my explanation for correct/incorrect answers
cannot find symbol
package problems...
How do i compile B.java?
Help with Package understanding