The Wayback Machine - https://web.archive.org/web/20241104024039/https://www.geeksforgeeks.org/difference-between-package-and-interface-in-java/
Open In App

Difference Between Package and Interface in Java

Last Updated : 15 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Similar Reads

Why to Use Comparator Interface Rather than Comparable Interface in Java?
In Java, the Comparable and Comparator interfaces are used to sort collections of objects based on certain criteria. The Comparable interface is used to define the natural ordering of an object, whereas the Comparator interface is used to define custom ordering criteria for an object. Here are some reasons why you might want to use the Comparator i
9 min read
Difference between Inheritance and Interface in Java
Java is one of the most popular and widely used programming languages. Java has been one of the most popular programming languages for many years. Java is Object Oriented. However, it is not considered as a pure object-oriented as it provides support for primitive data types (like int, char, etc). In this article, we will understand the difference
3 min read
Difference Between ReadWriteLock Interface and ReentrantReadWriteLock Class in Java
ReadWriteLock is an interface in java. A ReadWriteLock allows us to add a thread-safety feature to a data structure while increasing throughput by allowing multiple threads to read the data concurrently and one thread to update the data exclusively. ReadWriteLock is described in the java.util.concurrent.locks package, with ReentrantReadWriteLock is
3 min read
Difference Between poll() and remove() method of Queue Interface in Java
The queue is a data structure that stores elements in a first-in, first-out (FIFO) order. It can be used to implement a priority queue or as a basic synchronization primitive. The remove() method removes an element from the head of this queue and returns it. The poll() method blocks until one or more elements become available for removal, then retu
4 min read
Difference between Abstract Class and Interface in Java
In object-oriented programming (OOP), both abstract classes and interfaces serve as fundamental constructs for defining contracts. They establish a blueprint for other classes, ensuring consistent implementation of methods and behaviors. However, they each come with distinct characteristics and use cases. In this article, we will learn abstract cla
10 min read
Difference between Program and Package
1. Program : Program, as name suggest, is collection of instructions written in sequence that tells a computer what operation to perform and produce result one want as well as solve problem, increase creativity and improve performance. 2. Package : Package, as name suggests, are simply set of program files, data files, etc. developed by programmers
2 min read
Difference between tilde ( ~ ) and caret ( ^ ) in package.json
In package.json, the tilde (~) and caret (^) symbols are used to specify the version range for dependencies, controlling how updates are handled when you run npm install or yarn install. Tilde allows only the patch version upgrades avoiding the minor updates while caret allows updates to patch as well as minor versions. Table of Content Tilde (~) n
3 min read
Difference between Input and Output Interface Unit
Computer system operates the computation on the digital signals but the user sends analog signals to the computer system. Interface Units are basically used for the conversion of the signals in the computer system so that mutual communication and understanding can be established between environment and computer system. Interface Units are of two ty
2 min read
Difference between Parallel Virtual Machine (PVM) and Message Passing Interface (MPI)
1. Parallel Virtual Machine (PVM) : PVM is a type of software tool which is designed for heterogeneous distributed computing. This software was created at the Oak Ridge National Laboratory in 1989 and it was rewritten in 1991 at the university of Tennessee. In this system, it allows a heterogeneous network which can be made from a collection of dis
2 min read
Differences between Interface and Class in Java
This article highlights the differences between a class and an interface in Java. They seem syntactically similar, both containing methods and variables, but they are different in many aspects. Differences between a Class and an Interface The following table lists all the major differences between an interface and a class in Java language: Class In
4 min read
Java.lang.Package class in Java
Java 2 added a class called Package that encapsulates version data associated with a package. Package version information is becoming more important because of the proliferation of packages and because a java program may need to know what version of a package is available. This versioning information is retrieved and made available by the ClassLoad
8 min read
Java.util Package in Java
Java.util PackageIt contains the collections framework, legacy collection classes, event model, date and time facilities, internationalization, and miscellaneous utility classes (a string tokenizer, a random-number generator, and a bit array).Following are the Important Classes in Java.util package : AbstractCollection: This class provides a skelet
4 min read
Java.lang package in Java
Java.lang package in JavaProvides classes that are fundamental to the design of the Java programming language. The most important classes are Object, which is the root of the class hierarchy, and Class, instances of which represent classes at run time. Following are the Important Classes in Java.lang package : Boolean: The Boolean class wraps a val
3 min read
Java.io Package in Java
Java.io Package in JavaThis package provides for system input and output through data streams, serialization and the file system. Unless otherwise noted, passing a null argument to a constructor or method in any class or interface in this package will cause a NullPointerException to be thrown. Following are the important classes in Java.io package:
1 min read
Java 8 | DoubleToIntFunction Interface in Java with Example
The DoubleToIntFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and gives an int-valued result. The lambda expression assigned to an object of DoubleToIntFunction type is used to define
1 min read
Java 8 | IntToDoubleFunction Interface in Java with Examples
The IntToDoubleFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in an int-valued argument and gives a double-valued result. The lambda expression assigned to an object of IntToDoubleFunction type is used to define
1 min read
Java 8 | DoubleToLongFunction Interface in Java with Examples
The DoubleToLongFunction Interface is a part of the java.util.function package which has been introduced since Java 8, to implement functional programming in Java. It represents a function which takes in a double-valued argument and gives an long-valued result. The lambda expression assigned to an object of DoubleToLongFunction type is used to defi
1 min read
Java.util.function.BiPredicate interface in Java with Examples
The BiPredicate<T, V> interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on two objects and returns a predicate value based on that condition. It is a functional interface and thus can be used in lambda expression also. public interface BiPredicate<T, V> Methods: test(): This functio
2 min read
Java.util.function.DoublePredicate interface in Java with Examples
The DoublePredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on a Double object and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface DoublePredicate Methods test(): This function evaluates a co
2 min read
Java.util.function.LongPredicate interface in Java with Examples
The LongPredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on a long value and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface LongPredicate Methods test(): This function evaluates a condition
2 min read
Java.util.function.IntPredicate interface in Java with Examples
The IntPredicate interface was introduced in JDK 8. This interface is packaged in java.util.function package. It operates on an integer value and returns a predicate value based on a condition. It is a functional interface and thus can be used in lambda expression also. public interface IntPredicate Methods: test(): This function evaluates a condit
2 min read
java.net.FileNameMap Interface in Java
java.net.FileNameMap is a Java Interface. FileNameMap interface is a part of the java.net package. FileNameMap provides a mechanism to map between a file name and a MIME type string. We can use FileNameMap getContentTypeFor method to get the MIME type of the specified file. Syntax: public interface FileNameMapMethod of java.net.FileNameMap Interfac
2 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Java | Implementing Iterator and Iterable Interface
Iterators are used in Collection framework in Java to retrieve elements one by one. For more details and introduction related to this, see this link. Why it is needed to implement Iterable interface? Every class that implements Iterable interface appropriately, can be used in the enhanced For loop (for-each loop). The need to implement the Iterator
4 min read
Controlling the Visibility of Class and Interface in Java
Maintenance is one of the important aspects of software development, and experience has shown that software that maintains its component's visibility low is more maintainable than one that exposes its component more. You're not going to know it upfront, but when redesigning the application, you're going to miss it terribly. You end up patching and
5 min read
Java Spring - Using @PropertySource Annotation and Resource Interface
In Java applications, Sometimes we might need to use data from external resources such as text files, XML files, properties files, image files, etc., from different locations (e.g., a file system, classpath, or URL). @PropertySource Annotation To achieve this, the Spring framework provides the @PropertySource annotation as a facility to read and lo
6 min read
Differences between Interface and Integration Testing
Interface Testing: Interface Testing is a type of software testing type that checks the proper communication between two different software systems. Interface is the connection that integrates two components. The interface could be anything like APIs, web services etc. Testing of these connecting interfaces is defined as Interface Testing. An inter
2 min read
HTTP API of java.net.http Package With Examples
HTTP Client and WebSocket APIs provide high-level client interfaces to HTTP (versions 1.1 and 2) and low-level client interfaces to WebSocket. The main types defined are namely as follows: HttpClientHttpRequestHttpResponse The protocol-specific requirements are defined in the Hypertext Transfer Protocol Version 2 (HTTP/2), the Hypertext Transfer Pr
14 min read
javax.xml.crypto Package in Java
Common classes having the XML cryptography for the Package javax.xml.crypto. now this crypto package includes similar classes that are used to accessing XML cryptographic on its operations, such as creating an XML signature or encrypted XML data. There are two types of javax.xml.crypto classes in this package that are KeySelector class, which allow
4 min read
Package getSpecificationVendor() method in Java with Examples
The getSpecificationVendor() method of java.lang.Package class is used to get the vendor of the specification of this package. The method returns the specification vendor of the package as a String.Syntax: public String getSpecificationVendor() Parameter: This method does not accept any parameter.Return Value: This method returns the specification
2 min read
three90RightbarBannerImg