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
- Avoids Naming Conflicts: You can have a class named
Employeein thehrpackage and another class namedEmployeein theitpackage without any conflict. - Access Protection: Packages provide access control (using the default/package-private access modifier).
- 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). Thejava.langpackage 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?
- Multiple Inheritance: Java does not allow a class to
extendmore than one class. However, a class canimplementmultiple interfaces. - Abstraction: It forces the implementing class to provide the actual code for all the methods defined in the interface, creating a strict contract.
- 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
publicandabstract. - All variables are implicitly
public,static, andfinal(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
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Can have both abstract (no body) and concrete (with body) methods. | Only abstract methods (until Java 8 introduced default/static methods). |
| Variables | Can have non-final and non-static variables. | Variables are ALWAYS public static final (constants). |
| Inheritance | A class can extend only ONE abstract class. | A class can implement MULTIPLE interfaces. |
| Keyword | Uses abstract keyword. | Uses interface keyword. |
Course4All Editorial Board
Verified ExpertSubject Matter Experts
Comprising experienced educators and curriculum specialists dedicated to providing accurate, exam-aligned preparation material.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.