• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between Packages and Interfaces in Java

packages-and-interfacesPackages and Interfaces both acts as a container. The content in packages and interfaces can be used by the classes by importing and implementing it correspondingly.

The basic difference between packages and interfaces is that a package contains a group of classes and interfaces whereas, an interface contains methods and fields. Let’s study some other differences with the help of comparison chart.

Content: Packages Vs Interfaces in Java

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonPackagesInterfaces
BasicPackages is a group of classes and/or interfaces together.Interfaces is a group of abstract methods and constant fields.
KeywordPackages are created using "Package" keyword.Interface are created using "Interface" keyword.
Syntaxpackage package_name;
public class class_name{
.
(body of class)
.
}
interface interface_name{
variable declaration;
method declaration;
}
AccessA package can be imported An interface can be extended by another interface and implemented by the class.
Access keyword Packages can be imported using "import" keyword.Interfaces can be implemented using "implement" keyword.

Definition of Packages

Packages are collection or groups of the variety of classes and interfaces. The classes in packages are related to each other in some scope or by inheritance. You can also create your package and use it for your program.

Creating a package

For creating a package just follow the following steps.

  1. Open a file and then declare the name of the package at the top of the file, like [ package package_name; ] the package name is the name you want to give to the package.
  2. Next, you define a  class that you want to put in the package, and remember that you declare it public.
  3. Save the file as a .java file and then compile the file, then”  .class” is obtain for that file.
  4. To create a package for this file the command used is “javac -d . file_name.java. You can see that the package is created containing a ” .class” file in the current directory. To place it in parent directory use    “javac -d . . file_name.java” command.
  5. You can also create a subpackage by declaring subpackage name as  [ package package_name1. package_name2; ] at the top of the file.
package Mypackage;
public class myclass{
public void displayMypackage( ){
system.out.println("method displayMypackage of class myclass of package Mypackage");
}

Using the Package

The packages created or available in the directory can be used in the program by using an import statement.The keyword used to import any package in your program is “import”. The import statement can be written in two ways, or you can say that there are two ways to access any package.

First, if you want to use a particular class from a package, The “import” keyword is followed by the package name further followed by the dot operator and the class name which you want to use from the package. Second, if you want to use many classes that are contained in the packages, then the import keyword is followed by the package name further followed by the dot and the ” * ” operator.

import package_name . class_name;
or
import package_name . *;

In above code, you can see the * sign which indicates that second method imports all the classes contained in the packages.

Now, let’s view the use of the package with an example.

import Mypackage . myclass{
class TestMypackage{
public static void main(string args[ ]){
myclass ob1= new myclass( );
ob1.displayMypackage( );
}
}

//output
method displayMypackage of class myclass of package Mypackage.

In above code, the class TestMypackage has imported the package Mypackage and used its displayMypackage( ) method.

Definition of Interface

Interface is a kind of a class, but, differs in a sense that the methods declared in the interface are abstract that means the methods are only declared but not defined. The fields in the interface are always public, static, final. The fields must be initialized at the time of declaration.

The methods declared by the interface are defined by the class which implements that interface according to its requirement. As the methods in the interface do not perform any function, so there is no use of creating any object of the interface. Hence, no object can be created for the interface.

The interface can also inherit the other interface but, the class inheriting such an interface must also implement all the methods of the inherited interface. As the fields are initialized at the time of their declaration in the interface, so there is no need of constructor in the interface hence, the interface doesn’t contain any constructor. Let’s see the example of creating and using an interface.

interface Area {
float pi= 3.14;
float find_area(float a, float b){
}
class Circle implements Area{
float find_area(float a, float b){
return (pi*a*a);
}
Class Shapes{
public static void main(string args[ ]){
Area A=new Area ( );
Circle C= new circle ( );
A=C;
float F= Area. find_area(10,10);
system.out.println("Area of the circle is :" +F);
}

In above code, we had created an interface Area, and the class Circle has implemented the interface Area. The field “pi” has been initialized in the interface at the time of its declaration. The class Circle has defined the abstract method of the class area according to its requirement.

Key Differences Between Packages and Interfaces in Java

  1. A package is a group of classes and interfaces together whereas, an interface is a group of abstract methods.
  2. Package is created using a keyword package whereas, an interface is created using a keyword interface.
  3. If a class or an interface inside a package is to be used the package is to be imported while an interface has to be implemented.

Conclusion

Both packages and interface are the containers. Package reduces the size of the code as we just import the class to be used instead of again define it. Whereas the interface reduces the confusions occurred while multiple inheritances because in the case of multiple inheritances the inheriting class has not to decide that definition of which method it should inherit instead it defines its own.

Related Differences:

  1. Difference Between Applet and Servlet in Java
  2. Difference Between Float and Double
  3. Difference Between throw and throws in Java
  4. Difference Between Interface and Abstract Class in Java & C#
  5. Difference Between Comparable and Comparator in Java

Comments

  1. kapil says

    January 25, 2017 at 12:15 pm

    superb thank you so much for explaining the differences so well. May god bless you and give you the endurance to keep helping others.

    Reply
    • Neha K says

      January 27, 2017 at 4:15 am

      Thanks for your appreciation and blessings as well 🙂

      Reply
  2. Harika sailakshmi says

    April 9, 2017 at 12:14 pm

    It has given a clear explanation thank you

    Reply
  3. karthikeyan says

    August 28, 2019 at 9:15 am

    Thank you for the definition, completely cleared the doubts regarding interface and package. Thank you so much.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy