Java Packages

Packages in Java

In Java, a package is a way to organize related classes, interfaces, and sub-packages into groups, similar to a folder structure. It helps prevent naming conflicts by providing a unique namespace for your classes, making your code more organized and easier to manage. Packages also allow you to control access to your classes and methods, promoting better code structure.
For example, java.util is a package that contains utility classes like ArrayList, Date, etc.

Structure of a Package

package packageName;

[access_modifier] class ClassName {

    // Execution code like methods, fields, properties, constructors, etc.
}

Why Do We Use Packages?

Organization:

    ● Helps keep related classes and other types together.
    ● Makes the code easier to manage and understand.

Avoiding Name Conflicts

       ● Prevents conflicts when you have multiple classes with the same name in different parts of your program or when
       using libraries.

Example: Organizing Animals with Namespaces

Imagine you have a list of animals, some are pets and some are wild animals. We can use packages to organize them into different categories.

package pets;

public class Dog {
    public void makeSound() {
        System.out.println(“The dog says: Woof Woof”);
    }
}
package wildanimals;
public class Lion {
    public void makeSound() {
        System.out.println(“The lion says: Roar”);
    }
}
import pets.Dog;
import wildanimals.Lion;

public class Program {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        Lion myLion = new Lion();
        myDog.makeSound(); // Outputs: The dog says: Woof Woof
        myLion.makeSound(); // Outputs: The lion says: Roar
    }
}

Explanation:

// Define a package named pets

package pets;

// Define a class named Dog within the pets package

public class Dog {

// Method to make the dog sound

package wildanimals;

public void makeSound() {
System.out.println(“The dog says: Woof Woof”); // Prints the dog’s sound
}
}

// Define a class named Lion within the wildanimals package

public class Lion {

// Method to make the lion sound

public void makeSound() {
System.out.println(“The lion says: Roar”); // Prints the lion’s sound
}
}

// Import the Dog and Lion classes from their respective packages

import pets.Dog;
import wildanimals.Lion;

// Import the Dog and Lion classes from their respective packages

import pets.Dog;
import wildanimals.Lion;

// Main program class

public class Program {
    public static void main(String[] args) {
        // Create an instance of Dog from the pets package
        Dog myDog = new Dog();
        // Create an instance of Lion from the wildanimals package
        Lion myLion = new Lion();
        // Call the makeSound method for the dog
        myDog.makeSound(); // Outputs: The dog says: Woof Woof
        // Call the makeSound method for the lion
        myLion.makeSound(); // Outputs: The lion says: Roar
    }
}

Tasks:
1. Package Organization:
Create a package named com.mycompany.utilities.
Inside this package, create a class named MathFunctions with a static method calculateSquare(int number) that calculates and returns the square of the given number.
Use the package and class in the main method to calculate and print the square of a number like 10.

2. Import Statements:
Create a package named geometry.shapes.
Inside this package, create a class named Circle with a property radius and a method calculateArea() that calculates and returns the area of the circle.
Use the geometry.shapes package in the main method to create an object of the Circle class, set its radius, calculate the area, and print the result.

3. Package Name Conflict Resolution:
Create two packages: finance and hr.
Inside each package, create a class Employee with properties name, id, and a method displayDetails() that prints the details of the employee.
Use fully qualified package names to resolve any name conflicts, and create objects of both Employee classes in the main method, set their properties, and call the displayDetails() method.

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

This free course teaches Java packages, their types, and usage with practical examples.

You’ll learn the purpose of packages, types of packages (default and user-defined), and how to organize code using them.

Packages are containers that group related classes and interfaces together, helping to organize and manage code.

The default package is the unnamed package where classes are placed if no specific package is defined.

User-defined packages are custom packages created by developers to organize classes and avoid naming conflicts.

You can create a package using the package keyword at the beginning of your Java file.

Yes, the course covers how to use the import statement to access classes from different packages.

Packages improve code organization, reusability, and help avoid naming conflicts in large projects.

Yes, the course provides step-by-step examples of creating and using both default and user-defined packages.

Yes, it’s designed for beginners and includes simple explanations and practical examples.