Packages 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
Comparison Chart
Basis for Comparison | Packages | Interfaces |
---|---|---|
Basic | Packages is a group of classes and/or interfaces together. | Interfaces is a group of abstract methods and constant fields. |
Keyword | Packages are created using "Package" keyword. | Interface are created using "Interface" keyword. |
Syntax | package package_name; public class class_name{ . (body of class) . } | interface interface_name{ variable declaration; method declaration; } |
Access | A 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.
- 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.
- Next, you define a class that you want to put in the package, and remember that you declare it public.
- Save the file as a .java file and then compile the file, then” .class” is obtain for that file.
- 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.
- 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
- A package is a group of classes and interfaces together whereas, an interface is a group of abstract methods.
- Package is created using a keyword package whereas, an interface is created using a keyword interface.
- 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.
kapil says
superb thank you so much for explaining the differences so well. May god bless you and give you the endurance to keep helping others.
Neha K says
Thanks for your appreciation and blessings as well 🙂
Harika sailakshmi says
It has given a clear explanation thank you
karthikeyan says
Thank you for the definition, completely cleared the doubts regarding interface and package. Thank you so much.