Packages & Interfaces

Expert Answer & Key Takeaways

Organizing Java code using Packages and achieving multiple inheritance and 100% abstraction using Interfaces.

Packages and Interfaces in Java

As Java applications grow large, managing hundreds of classes becomes difficult. Java provides Packages to organize classes into folders. Furthermore, since Java does not support multiple inheritance with classes, it provides Interfaces to overcome this limitation.

1. Packages in Java

A package in Java is a mechanism to encapsulate a group of classes, sub-packages, and interfaces. It is essentially a folder or directory structure.

1.1 Advantages of Packages

  1. Avoids Naming Conflicts: You can have a class named Employee in the hr package and another class named Employee in the it package without any conflict.
  2. Access Protection: Packages provide access control (using the default/package-private access modifier).
  3. Easier Searching: Code is organized and easier to locate.

1.2 Types of Packages

  • Built-in Packages: Provided by the Java API (e.g., java.lang, java.util, java.io). The java.lang package is automatically imported by default in every Java program.
  • User-defined Packages: Created by the programmer.

1.3 Creating and Using a Package

To put a class inside a package, the package keyword must be the very first non-comment line in the source file.
package com.mycompany.utils; public class Calculator { public int add(int a, int b) { return a + b; } }
To use this class in another file, you must import it:
import com.mycompany.utils.Calculator; // Or import everything in the package: // import com.mycompany.utils.*;

2. Interfaces in Java

An interface in Java is a blueprint of a class. It is a collection of abstract methods (methods without a body) and static constants. It is used to achieve 100% Abstraction and Multiple Inheritance.

2.1 Why use Interfaces?

  1. Multiple Inheritance: Java does not allow a class to extend more than one class. However, a class can implement multiple interfaces.
  2. Abstraction: It forces the implementing class to provide the actual code for all the methods defined in the interface, creating a strict contract.
  3. Loose Coupling: Code interacting through interfaces is loosely coupled, making it easier to maintain and test.

2.2 Interface Rules (Pre-Java 8)

  • All methods are implicitly public and abstract.
  • All variables are implicitly public, static, and final (they are constants).
  • You cannot instantiate an interface (you cannot create an object using new InterfaceName()).

2.3 Creating and Implementing an Interface

// Defining an interface interface Animal { void eat(); // Automatically public and abstract void sleep(); } // Implementing the interface class Dog implements Animal { // Must provide implementation for all methods public void eat() { System.out.println("Dog eats bones"); } public void sleep() { System.out.println("Dog sleeps"); } }

2.4 Multiple Inheritance via Interfaces

interface Printer { void print(); } interface Scanner { void scan(); } // One class implementing MULTIPLE interfaces class MultiFunctionDevice implements Printer, Scanner { public void print() { System.out.println("Printing..."); } public void scan() { System.out.println("Scanning..."); } }

3. Abstract Class vs. Interface

FeatureAbstract ClassInterface
MethodsCan have both abstract (no body) and concrete (with body) methods.Only abstract methods (until Java 8 introduced default/static methods).
VariablesCan have non-final and non-static variables.Variables are ALWAYS public static final (constants).
InheritanceA class can extend only ONE abstract class.A class can implement MULTIPLE interfaces.
KeywordUses abstract keyword.Uses interface keyword.

Course4All Editorial Board

Verified Expert

Subject Matter Experts

Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.

Pattern: 2026 Ready
Updated: Weekly