A set of classes and interfaces grouped together are known as Packages
in Java
- Organize grioup of classes into single API Unit
- auto control naming conflicts
- easy access protection
- 2 access levels: Protected, Default
Java Package Subpackage Class units
Other 2 major types: BuiltIn , UserDefined
Java.awt.event
:
- Java: Package
- awt: sub package
- event: class
come along with JDK use import statement
package Example;
import java.util.ArrayList;
class buildinpackage{
psvm{
// define array, insert elements, print array
}
}
import package.*
(available for both BI and UD)import package.classname
- some fully qualified name, without
import
fully qualified like:
class myarray implements java.util.ArrayList{
// something
}
makes only the declared class of the package accessible adv when two packages have same class name
- use package command as the starting statement, like
package Example
- further define all necessary classes
creating a class inside a package while importing another package is also valid
to define a subpackage, modify package command acc, like
package ExamplePackage.multiply
Private Package: can only access private modifier within the package
Protected Package: can only access directly from the class and thru classes from child class. They make use of the inheritance
concept
Public: can be accessed anywhere (within class and outside class)
Access Modifier | Access in Class | Access in Package | Access outside package using subclass | Access directly outside class |
---|---|---|---|---|
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |