How do packets differ from interfaces

Difference between packages and interfaces in Java

Packages and interfaces both act as containers. The contents in packages and interfaces can be used by the classes by importing them and implementing them accordingly. The basic difference between packages and interfaces is that a package contains a set of classes and interfaces, while an interface contains methods and fields. Let's examine some other differences using a comparison table.

Comparison table

Basis of comparisonPackagesInterfaces
basicPackages are a group of classes and / or interfaces.Interfaces are a group of abstract methods and constant fields.
keywordPackages are created with the keyword "Package".The interface is created with the keyword "interface".
syntaxPackage package name;
public class class name {
(Class of class)
Interface interface name {
variable statement;
Method declaration;
accessA package can be importedAn interface can be extended by another interface and implemented by the class.
Access to keywordPackages can be imported with the keyword "import".Interfaces can be implemented with the keyword "implement".

Definition of packages

Packages are collections or groups of the various classes and interfaces. The classes in packages are linked to one another to some extent or by inheritance. You can also create your package and use it for your program.

Create a package

To create a package, just follow the steps below.

  1. Open a file and add the name of the package to the top of the file, such as [package package_name; ] The package name is the name you want to give the package.
  2. Next, you'll define a class that you want to put in the package, and remember to make it public.
  3. Save the file as a .java file and compile the file. It then gets .class for that file.
  4. To package this file, use the command “javac -d. You can see that the package has been built and has a ".class" file in the current directory. To save it in the higher-level directory, use “javac -d. . Command ”.
  5. You can also create a sub-package by specifying the sub-package name as [package package-name1]. package_name2; ] at the beginning of the file.
Package Mypackage; public class myclass {public void displayMypackage () {system.out.println ("method displayMypackage of class myclass of the package Mypackage"); }

Use the package

The packages created or available in the directory can be used in the program using an import statement. The keyword for importing a package in your program is "import". The import statement can be written two ways, or you can say there are two ways to access each package. If you want to use a specific class from a package, the keyword "import" is first followed by the package name, followed by the dot operator and the class name you want to use from the package. If you want to use many of the classes that are included in the packages, the import keyword is followed by the package name, followed by the period and the "*" operator.

import package name. Class name; or import package name. *;

In the code above, you can see the * character indicating that the second method will import all of the classes in the packages.

Now let's look at the usage of the package with an example.

Import my package. myclass {class TestMypackage {public static void main (string args []) {myclass ob1 = new myclass (); ob1.displayMypackage (); }} // The displayMypackage output method of the myclass class of the Mypackage package.

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

Definition of the interface

Interface is a kind of class, but differs in that the methods declared in the interface are abstract, ie the methods are only declared, but not defined. The fields in the user interface are always public, static and final. The fields must be initialized at the time of declaration. The methods declared by the interface are defined by the class that implements this interface according to its requirements. Since the methods in the interface do not carry out any functions, no objects of the interface can be created. Therefore, no object can be created for the interface.

The interface can also inherit the other interface, but the class that inherits such an interface must also implement all of the methods of the inherited interface. Since the fields are initialized when they are declared in the interface, no constructor is required in the interface. Therefore the interface does not contain a constructor. Let's look at the example of how to create and use an interface.

Interface area {float pi = 3, 14; float find_area (float a, float b) {} class implements circular area {float find_area (float a, float b) {return (pi * a * a); } Class forms {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 the code above, we had created an interface section, and the Circle class implemented the interface section. The "pi" field was initialized when it was declared in the interface. The class Circle has defined the abstract method of the class area according to its requirements.

Main differences between packages and interfaces in Java

  1. A package is a group of classes and interfaces, while an interface is a group of abstract methods.
  2. The package comes with a keyword package created, while creating an interface with a keyword interface becomes .
  3. If a class or an interface is to be used within a package, the package must be imported while an interface must be implemented.


Both packages and interfaces are the containers. The package reduces the size of the code as we are only importing the class to be imported instead of redefining it. While the interface reduces the confusion that has arisen during multiple inheritance, as with multiple inheritance the inheriting class doesn't have to decide which method to inherit, instead it defines its own.