← Back to Home

User-Defined Packages

User-defined packages are packages created by developers to organize application classes and interfaces in a logical, modular, and maintainable structure. They are essential for real-world Java projects, team collaboration, and large codebases. This is a common interview topic, often asked after built-in packages.

What Is a User-Defined Package?

  • A package created by the developer
  • Groups related classes and interfaces
  • Provides namespace and access control
  • Improves code organization and reusability
package com.mycompany.project.util;
          

Why User-Defined Packages Are Needed

  • Organize large applications
  • Avoid class name conflicts
  • Improve readability
  • Support layered architecture
  • Enforce access control using modifiers

Creating a User-Defined Package

1️⃣ Using package Keyword (Source Code Level)

package com.example.util;
public class MathUtil {
    public static int add(int a, int b) {
        return a + b;
    }
}
          

Rules:

  • package statement must be first line
  • Only one package per class

2️⃣ Compile and Create Directory Structure

javac -d . MathUtil.java
          

✔ Creates:

com/
 └── example/
     └── util/
         └── MathUtil.class
          

Accessing User-Defined Package Classes

1. Using Fully Qualified Name

com.example.util.MathUtil.add(2, 3);
          

2. Using import Statement

import com.example.util.MathUtil;
MathUtil.add(2, 3);
          

3. Using Wildcard Import

import com.example.util.*;
          

⚠️ Imports only classes in that package, not sub-packages

Package Naming Conventions (Very Important)

  • All lowercase
  • Reverse domain naming
  • Descriptive hierarchy
com.company.project.layer
          

Examples:

  • com.amazon.payment.service
  • org.apache.commons.lang

User-Defined Packages and Access Modifiers

Modifier Same Class Same Package Subclass (diff pkg) Everywhere
private
default
protected
public

✔ Packages define visibility boundaries

Sub-Packages (Clarification)

com.bank
 └── account
 └── loan
          
  • ✔ Sub-packages are independent
  • ❌ No automatic access to parent package members

Creating Multiple Classes in Same Package

package com.app.service;
public class UserService { }
class HelperService { }
          
  • ✔ Only one public class per file
  • ✔ Others have default access

Packaging into JAR (Real-World Usage)

jar cf app-utils.jar com/example/util/*.class
          
  • ✔ Distribute and reuse packages
  • ✔ Used in libraries and frameworks

Common Beginner Mistakes

  • Forgetting package statement
  • Incorrect folder structure
  • Using uppercase letters in package names
  • Assuming sub-packages inherit access
  • Overusing wildcard imports

Interview-Ready Answers

Short Answer

A user-defined package is a package created by developers to organize application classes.

Detailed Answer

In Java, user-defined packages are created using the package keyword to group related classes and interfaces. They help organize large applications, avoid naming conflicts, and enforce access control. Developers commonly follow reverse domain naming conventions when creating packages.

Key Takeaway

User-defined packages bring structure and scalability to Java applications. They are essential for clean architecture, modular development, and enterprise-level Java projects.