A package-private class is a class declared without any access modifier. Such a class is visible only within its own package and cannot be accessed directly from classes located in different packages. This restriction is part of Java's access control mechanism designed to enforce encapsulation and module boundaries.
Key Points: • A package-private class can only be used by classes within the same package. • Classes from other packages cannot import or instantiate a package-private class directly. • To expose its functionality outside the package, you can make the class public or provide access through a public wrapper class, interface, or factory method.
Example: Suppose an internal utility class contains sensitive business logic that should only be used within a specific module. Keeping it package-private prevents external packages from depending on its implementation while allowing controlled access through a public service class.
Interview Tip: A concise interview answer is: A package-private class cannot be accessed directly from another package because its visibility is limited to the package where it is declared. To use it externally, you must either make the class public or expose its functionality through a public class or interface within the same package.