Packages in Java
Packages in Java
• Preventing naming conflicts. For example there can be two classes with name
Employee in two packages, college. staff. Cse . Employee and college . staff.
ee . Employee
• Making searching/locating and usage of classes, interfaces, enumerations and
annotations easier
• Providing controlled access: protected and default have package level access
control. A protected member is accessible by classes in the same package and
its subclasses. A default member (without any access specifier) is accessible
by classes in the same package only.
• Packages can be considered as data encapsulation (or data-hiding).
How packages work?
• Packages help organize your Java code and prevent naming conflicts.
If you’re looking to master package creation and management in
large Java applications, the Java Programming Course provides
comprehensive lessons and hands-on exercises
• java.lang
• java.io
• java.util
• java.applet
• java.awt
• java.net
User-defined packages
obj.getNames(name);
}
}
• Note : MyClass.java must be saved inside the myPackage directory since it is a part of the
package.