Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to protect classes so they are not visible outside their package

I'd like to be able to have two "protected" classes in my package. That is, I do not want files outside of my package to see them as visible - they will be for internal use within the package only.

How can I do this?

like image 768
Cam Avatar asked Mar 28 '10 21:03

Cam


People also ask

Can a class be package private?

package-private is the default access modifier and does not have a keyword, because package is used to specify the package for a class or interface. To declare package access for something, use no access modifier. protected indicates that only descendants of the class can access the item.

Can we access a class outside of package?

If a class is declared as default then we can access that class only within the current package i.e from the outside package we can't access it. Hence, the default access modifier is also known as the package–level access modifier. A similar rule also applies for variables and methods in java.

Which keyword can protect a class in a package from accessibility by the classes outside the package?

Which keyword can protect a class in a package from accessibility by the classes outside the package? static.


1 Answers

Just leave out all keywords. The default visibility is package-private, viewable within the package only.

e.g.:

// class Foo is public public class Foo {     final private Bar bar = ...; }  // class Bar is package-private // (visible to all classes in the package, not visible outside the package) class Bar {     ...; } 
like image 75
Jason S Avatar answered Sep 20 '22 20:09

Jason S