What are Java packages?
We all began from somewhere in our journey to learning how to code and software development in general. One characteristic of seasoned developers is not that they can write codes or their thought processes, it is that their foundational knowledge is solid. They know this stuff. So, any beginner Java programmer aspiring to be great should get an excellent grasp of the basics, especially Java Packages.
When you see the term “package,” don’t think of it as jargon to deceive you. Instead, think of folders and folder structures, because thats what a package is in the most basic form. Java packages are clusters of comparable varieties of categories, interfaces, and sub-packages. It is a mechanism for encapsulating teams of Java packages, classes, sub-packages, and interfaces.
Types of Java packages
There are essentially two types of Java packages, namely the User-defined packages and the built-in packages.
Built-in package
The built-in package consists of the many categories that constitute a part of the Java API. A number of the inherent packages are:
java.lang: Contains language support categories (for example, categories that outline default knowledge varieties, like math).
java.io: Contains categories to support input / output operations.
java.util: Contains utility categories for implementing knowledge structures like a List of collections and dictionaries to support date/time work.
java.applet: Contains categories for making applications programms.
java.awt: Contains categories for implementing elements of graphical user interfaces (for example, buttons, menus, etc.).
java.net: Contains categories that support network operations.
User-defined packages
As the name suggests, these packages are user-defined. We create a folder whose name should be the same as the name of the package. Next, we create a class inside the folder.
Package names and directory structure are closely related. For example, if a package name is school.faculty.department, then there are three directories school, faculty, and department. The school package can be thought of as a tier one package, while faculty is a school sub-package and department is a sub-package of faculty.
Why do we need packages in Java?
Java packages make it easy to find or locate classes and interfaces.
Java packages help to avoid name conflicts. For example, there could be two classes with the name Student in two packages, school.scienceDepartment.Student and school.educationDepartment.Student.
Java packages perform data encapsulation (or data masking).
Java packages provide controlled access. Access designations are protected and have packet-level access control by default. A member declared as guard is accessible by classes of the same package and its subclasses. A member without an access specifier is a default identifier that can only be accessed by classes in the same package.
Java packages reuse classes contained in packages from other programs.
Java packages provide a unique comparison of classes in other packages.
Top comments (0)