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?
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.
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? static.
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 { ...; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With