• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Access modifiers

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why isn't there any C.class in your bin directory ? Maybe you forgot to recompile A.java ?
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Marshal
Posts: 79967
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote: . . . Do you need to set your classpath?

I don't think so.
 
Marcelo Vieira
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Destiny's powerful hand has made the bed of my future. And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic