Difference between packages and interfaces in Java. Also give suitable examples.

Difference Between Packages and Interfaces in Java (with Examples)

In Java programming, packages and interfaces serve different purposes: packages organize code, while interfaces define behavior contracts. Understanding both helps you write clean, modular, and maintainable programs—an essential skill for BTech CSE students in the 5th semester.

What is a Package in Java?

A package in Java is a namespace that groups related classes, interfaces, and sub-packages. It helps avoid name conflicts, supports access control, and makes code easier to manage and reuse.

  • Purpose: Organize and group related code.
  • Prevents naming collisions using unique namespaces (like java.util).
  • Enables controlled access (public/package-private).
  • Supports distribution and reuse (e.g., JAR files).
  • Follows directory structure (package name = folder path).

Simple Package Example

// File: src/com/college/math/Calculator.java
package com.college.math;

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

// File: src/com/college/app/Main.java
package com.college.app;

import com.college.math.Calculator;

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println("Sum = " + calc.add(5, 7));
    }
}

How to Compile and Run (Packages)

// From project root
javac -d out src/com/college/math/Calculator.java src/com/college/app/Main.java
java -cp out com.college.app.Main

What is an Interface in Java?

An interface is a contract that specifies what a class must do, not how it does it. A class implements an interface by providing method bodies for the abstract methods declared in the interface.

  • Purpose: Define behavior contracts for classes.
  • Contains abstract methods (and from Java 8: default and static methods; from Java 9: private methods inside interfaces).
  • Fields are implicitly public static final (constants).
  • Cannot be instantiated; implemented by classes.
  • Supports multiple inheritance of type: a class can implement multiple interfaces.

Interface Example with Multiple Implementation

// File: src/com/college/vehicles/Drivable.java
package com.college.vehicles;

public interface Drivable {
    void start();
    void stop();
    default int wheels() { // default method (Java 8+)
        return 4;
    }
}

// File: src/com/college/vehicles/Electric.java
package com.college.vehicles;

public interface Electric {
    int batteryLevel();
}

// File: src/com/college/vehicles/Car.java
package com.college.vehicles;

public class Car implements Drivable {
    @Override
    public void start() { System.out.println("Car started"); }

    @Override
    public void stop() { System.out.println("Car stopped"); }
}

// File: src/com/college/vehicles/ElectricCar.java
package com.college.vehicles;

public class ElectricCar implements Drivable, Electric {
    private int battery = 80;

    @Override
    public void start() { System.out.println("Electric car ON"); }

    @Override
    public void stop() { System.out.println("Electric car OFF"); }

    @Override
    public int batteryLevel() { return battery; }
}

// File: src/com/college/app/Demo.java
package com.college.app;

import com.college.vehicles.*;

public class Demo {
    public static void main(String[] args) {
        Drivable d = new ElectricCar(); // program to the interface
        d.start();
        System.out.println("Wheels: " + d.wheels());

        Electric e = (Electric) d; // interface reference
        System.out.println("Battery: " + e.batteryLevel() + "%");

        d.stop();
    }
}

How to Compile and Run (Interfaces)

javac -d out src/com/college/vehicles/*.java src/com/college/app/Demo.java
java -cp out com.college.app.Demo

Key Differences Between Packages and Interfaces

  • Purpose:
    • Package: Organizes types (classes, interfaces) into namespaces.
    • Interface: Specifies a set of behaviors that implementing classes must provide.
  • Contains:
    • Package: Classes, interfaces, enums, annotations, sub-packages.
    • Interface: Method signatures, constants, and optional default/static methods.
  • Usage keyword:
    • Package: Declared with package, used via import.
    • Interface: Declared with interface, used via implements (or extends for interface-to-interface).
  • Relationship:
    • Package: Structural grouping in the file system.
    • Interface: Type relationship enabling polymorphism.
  • Multiple inheritance:
    • Package: Not applicable.
    • Interface: A class can implement multiple interfaces.
  • Instantiation:
    • Package: Not instantiable.
    • Interface: Not instantiable; implemented by classes/extended by interfaces.
  • Access control:
    • Package: Controls default (package-private) access among types in the same package.
    • Interface: Members are public by default; fields are constants.
  • Naming and structure:
    • Package: Lowercase dot-separated names mapping to folders (e.g., com.college.app).
    • Interface: PascalCase names like classes (e.g., Drivable).

When to Use What

  • Use packages to organize your project modules, avoid name clashes, and manage visibility.
  • Use interfaces to define APIs, enable loose coupling, support polymorphism, and allow multiple behaviors via multiple interfaces.

In short: a package is a folder-like namespace for organizing code, while an interface is a contract that classes agree to implement. Both together help you design clean, scalable Java applications.