Packages & Modules
Packages
Examine Java package namespacing, directory structure mappings, and package boundary access control rules.
Interview: Focuses on unnamed default packages, directory-to-package structure mappings, and package access boundaries.
A Package in Java is a namespace system that groups related classes, interfaces, and sub-packages. Packages prevent name collisions, manage access controls, and map directly to OS directory hierarchies.
Namespace Protection
Allows classes with identical names (e.g. Date) to coexist in the codebase by placing them in distinct package paths.
Directory Mapping
Maps 1:1 with directories. A class declared in package com.company.db must sit in folder com/company/db.
Access Boundaries
Members declared with default (package-private) access can only be read by classes inside the same package folder.
Package Statement Rules and Directory Structures
The package statement must be the first line of code in a Java source file (excluding comments). The Java compiler and runtime enforce directory mapping:
- Naming Convention: Use reverse domain names (e.g.,
com.company.billing) to guarantee global uniqueness across libraries. Package names should be written in lowercase. - Compile Path Output: The compiler outputs class files structured in matching subfolders. Running
javac -d bin src/Main.javacreates the package directory tree insidebinautomatically.
Common Pitfalls
- Using the Default (Unnamed) Package: Placing source files without a package statement. Such classes cannot be imported or accessed by classes that reside in named packages.
- Mismatched Folder Hierarchies: Having a package declaration that does not match the actual folder structure, resulting in compile-time errors during class path resolution.
Best Practices
- Always Use Packages: Never place production classes in the default package. Keep them grouped in explicit, named packages.
- Leverage Package Boundaries: Use default (package-private) access for helper classes that should not be exposed outside of the package module API.
Interview-Relevant Information
Q1: What happens if you do not declare a package statement in a class?
Answer: The class is placed in the unnamed (default) package. While it can access other classes in the unnamed package, classes residing in named packages cannot import or reference it, making it unusable for structured APIs.
Q2: Do sub-packages inherit access permissions from their parent packages?
Answer: No. Packages in Java are completely flat. The package com.company.billing.utils is treated as completely distinct from com.company.billing. Classes in the sub-package cannot access default-scoped members of the parent package.
Quick Checklist
Can you map package definitions to operating system directories, explain why unnamed default packages are avoided, and describe sub-package access boundaries? If yes, you understand packages.
Use Cases
Structuring large multi-module codebases to avoid class name collisions.
Restricting internal components from leakage using package-private default visibilities.
Common Mistakes
Mismatched package declarations and folder names, leading to classloader lookup failures.
Declaring classes inside the default package for production builds, preventing modular class imports.