• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

default package

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If we have a class with a default access level in the default package (i.e. not in any package).

Is this class accessible to classes in all packages just as long as the class is in the classpath?

What about protected members in this class (which are only accessible to subclasses and other classes in the same package)? Or would these members only be accessible to other classes in the "default package"?

Thanks,
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jones,


Is this class accessible to classes in all packages just as long as the class is in the classpath?



Yes. The class may be used as any other class existing in java.lang package. However, Sun strongly recommends you to do not put your classes in default package.


What about protected members in this class (which are only accessible to subclasses and other classes in the same package)? Or would these members only be accessible to other classes in the "default package"?



The rule for accessing protected members of classes in default package is the same as if they are in some customized package. In this case, the protected member can be accessed for any class in default package and any class in other package through inheritance.

Please let me give you an additional information about classes in default package : For the exam, you must be aware about the difference when using the argumento -classpath to compile files which references classes in or out default package.

This is because if you opt to inform the -classpath argument during the compilation of a given source, informing the argument -cp . works a little bit different for classes which are in default package when comparing to those which are in a customized package.

For classes that are in the default package, you don't need to compile your source code from a parents directory where the .class file is, on the other hand, if such .class file is in a package and if you inform the argument -cp ., you have to compile from the parent directory of root directory of the package.

I think you get the point.

Please let me know if you need some more explanation about that.

Good luck in your study journey.
[ April 20, 2006: Message edited by: Edisandro Bessa ]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another thing is that the protected modifier in the default package is redundant as you may know that the default package can't be used in other packages as it doesn't have a qualified name for access.
 
I've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic