Difference Between Package and Interface in Java
Last Updated :
15 Nov, 2023
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.
|
Organize related classes and interfaces into a single unit.
| Define a contract for classes to implement.
|
Declared using package keyword.
| Declared using interface keyword.
|
Group classes and interfaces based on functionality.
| Define method signatures that classes implementing the interface must provide implementations.
|
Packages can have access modifiers to control visibility.
| Interface methods are implicitly public and abstract.
|
Does not support multiple inheritance.
| Supports multiple inheritance as a class can implement multiple interfaces.
|
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
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). I
3 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
9 min read
Difference between interfaces and classes in TypeScript In this article, we will see what is the Difference between "interface" and "Classes" in TypeScript. Interface: Interface is the virtual structure that is used for type-checking. In TypeScript we use interface keyword to create the new interface with identity. It create the structure for the same da
3 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 in
2 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 progra
2 min read