What is package in JAVA?
Java packages help in organizing multiple modules.
It helps in resolving naming conflicts when different packages have classes with the same names.
The access level of the packages helps in protecting data from being used by the non-authorized classes.
Packages group together related classes and interfaces. Packages avoid name conflicts. Classes in java are group together in a package using "package" keyword.
Example:package P1;
public class Class1
{
}
public class Class2
{
}
Advantages and disadvantages of using packages in Java.
Advantages:1. Determination of a category of a file is simplified.
2. Name space collision is avoided.
3. Access restriction can be applied with the use of packages.
4. Java packages can be stored in compressed files called JAR files.
Disadvantages:1. We cannot pass parameters to packages.
2. Change in a function needs to be reflected in all the functions that use the changed function and hence the whole package needs to be recompiled.