What is Package in java
A package is a namespace that organizes a set of related classes and interfaces.
Conceptually you can think of packages as being similar to different folders on your computer.
You might keep HTML pages in one folder, images in another, and scripts or applications in yet another.
Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
Why use?
Organization: Keeps related classes together
Avoid name conflict: if two classes with same name can exist in different packages.
Access Control: You can set visibility (like public, protected) using package.
Reusability: Easier to use classes in other projects.
Types of Packages in Java:
Built-in Packages:
These are predefined collections of classes and interfaces provided by Java to perform common tasks.
** Examples include**:
- java.util
- java.io
- java.lang
User-defined Packages:
These are packages Created by developers to group related classes and interfaces Code, helping to Organize and avoid name Conflicts.
Developers Create Custom packages to Structure their applications in a logical manner.
Eg:
package mypackage;
public class Message {
public void showMessage() {
System.out.println("Hello from mypackage!");
}
}
Top comments (0)