Open In App

Difference Between Package and Interface in Java

Last Updated : 15 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, packages and interfaces play crucial roles in organizing and structuring code. They serve different purposes and are used in distinct contexts. In this article, we will learn the concepts of the packages and interfaces in Java. syntax provides examples for each and then presents a table highlighting the key differences between them.

Difference Between Package and Interface in Java

The difference between the package and interface is tabularized below with a description.

Characteristics

Package

Interface

Purpose

Organize related classes and interfaces into a single unit.

Define a contract for classes to implement.

Syntax

Declared using package keyword.

Declared using interface keyword.

Usage

Group classes and interfaces based on functionality.

Define method signatures that classes implementing the interface must provide implementations.

Accessibility

Packages can have access modifiers to control visibility.

Interface methods are implicitly public and abstract.

Multiple Inheritance

Does not support multiple inheritance.

Supports multiple inheritance as a class can implement multiple interfaces.

Example

package com.geeksforgeeks.mypackage;

public interface Printable {
void print();
}

Package in Java

A package in Java is a mechanism for organizing related classes and interfaces into a single unit. It provides a way to group classes and interfaces based on their functionality making it easier to manage and maintain a large codebase.

Syntax

package com.geeksforgeeks.mypackage;

Example of Java Package

Below is the implementation of Java Package:

Java




package com.geeksforgeeks.mypackage;
import java.io.*;
  
public class GFG {
    public void display() {
        System.out.println("Hello from MyClass in com.geeksforgeeks.mypackage.");
    }
}


Output :

Hello from MyClass in com.example.mypackage.

Note: Generally these package names are the same as the website names in reverse order. For this website write.geeksforgeeks.org the package name will be org.geeksforgeeks.write

Interface in Java

An interface in Java defines a contract that a class must adhere to. It contains method signatures without implementations. The Classes that implement an interface must provide concrete implementations for all the methods declared in that interface. The Interfaces are used to achieve abstraction and support multiple inheritances in Java.

Syntax

public interface MyInterface {
// Method signatures (without implementations)
void method1();
void method2();
}

Example of Interface in Java

Below is the implementation of Interface in Java:

Java




interface MyInterface {
    void myMethod();
}
class MyClass implements MyInterface {
    public void myMethod() {
        System.out.println("Method implementation in MyClass.");
    }
}
public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.myMethod();
    }
}


output :

Method implementation in MyClass.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads