Packages

Perfect β€” let’s explore Packages in Java next. This topic is essential for modular design, code organization, and access control in large applications.


🧠 What Are Packages in Java?

A package in Java is like a folder in your system that groups related classes and interfaces together. It helps organize code, avoid class name conflicts, and control access levels.

Think of it like arranging your Java files in folders, so things don’t get messy as your project grows.


πŸ” Why Use Packages?

BenefitExplanation
βœ… ModularityRelated classes stay together logically and physically.
βœ… Avoid Naming ConflictsTwo classes with the same name can live in different packages.
βœ… Access ControlYou can use protected and default (package-private) visibility.
βœ… Reusable CodeEasily reuse utility or domain packages across projects.

πŸ”§ Types of Packages

  1. Built-in Packages: Provided by the Java SDK.
    • Examples: java.util, java.io, java.lang, etc.
  2. User-defined Packages: You create your own to organize your codebase.

🧱 Example: Creating and Using a Package

Step 1: Create a User-Defined Package

πŸ“„ File: mypackage/MessagePrinter.java

package mypackage;

public class MessagePrinter {
    public void print() {
        System.out.println("Hello from the package!");
    }
}

πŸ”Έ package mypackage; must be the first line in the file.


Step 2: Use the Package in Another File

πŸ“„ File: Main.java

import mypackage.MessagePrinter;

public class Main {
    public static void main(String[] args) {
        MessagePrinter printer = new MessagePrinter();
        printer.print();
    }
}

βœ… Compile & Run:

From the directory above mypackage, run:

javac mypackage/MessagePrinter.java
javac Main.java
java Main

πŸ” Package and Access Modifiers

  • Default (no modifier): Class is accessible only within its own package.
  • protected: Accessible in the same package and in subclasses even if they’re in different packages.
  • public: Accessible from everywhere.
  • private: Still only within the same class, unaffected by packages.

This makes packages essential for implementing Java’s access control and encapsulation mechanisms.


βš–οΈ Best Practices

  • Use meaningful package names, often based on reverse domain structure (e.g., com.myapp.services).
  • Group related classes (e.g., controllers, models, services, utils).
  • Avoid mixing unrelated classes in one package.
  • Use access modifiers effectively to protect internals of your package.

🏠 Homework for Students

  1. Create a package named university containing the following classes:

    • Student with name, rollNumber and a method displayDetails().
    • Teacher with name, subject and a method displayDetails().
  2. In a separate file (outside the package), import both classes and:

    • Create objects for each.
    • Call their displayDetails() method.
  3. Try to:

    • Access a class with default visibility from another package.
    • Use protected and public modifiers and observe access behavior.

Would you like to cover Access Modifiers next, or dive into Static Keyword or Interface vs Abstract Class?