What is the Difference Between Package and Interface

The main difference between package and interface is that a package is a collection of related classes and interfaces while an interface is a collection of fields and abstract methods.

Package and interface are two main concepts in Object Oriented Programming based languages such as Java. A package has a set of associated classes and interfaces. An interface has a set of fields and abstract methods.

Key Areas Covered

1. What is Package
     – Definition, Functionality
2. What is Interface
     – Definition, Functionality
3. What is the Relationship Between Package and Interface
     – Outline of the Association
4. What is the Difference Between Package and Interface
     – Comparison of Key Differences

Key Terms

Class, Interface, Package

Difference Between Package and Interface - Comparison Summary

What is Package

A package is a group or a collection that consist of related classes and interfaces. There are two types of packages in a programming language. They are the built-in packages and user-defined packages. The programming language Java consist of built-in packages such as lang, awt, javax, swing, net, io, util, sql, etc. The programmer can use the classes and interfaces in these packages on his program. He can also create his own packages which are the user-defined packages. A package provides a number of advantages. It helps to organize the classes and interfaces and improve maintainability. It also removes access protection and naming collision.

Difference Between Package and Interface

Moreover, it is possible to use the package using the import statement. If the programmer wants to use a specific class from a package, then he can use the package name with the dot operator and the required class name. For example, if the package name is test and the class name is Rectangle, then the import statement is as follows.

import test.Rectangle;

If the programmer wants to use many classes and interfaces in the package, then he can use import keyword with the package name, dot operator and * operator. For example, if the package name is “test”, the import statement is as follows.

import test. *;

What is Interface

Interface is a mechanism of achieving abstraction and multiple inheritance. The methods in an interface are abstract methods. These methods do not have any implementation. It can also have variables or fields. They can be public, static or final. Moreover, it is not possible to create objects using an interface. An interface can inherit other interfaces. If a class inherits a particular interface, that class has to implement all the methods of that inherited interface. In addition, there is no constructor in an interface.

Relationship Between Package and Interface

  • There can be multiple interfaces in a package.

Difference Between Package and Interface

Definition

A package is an organized set of related classes and interfaces whereas an interface is a set of fields and abstract methods that mainly allows implementing abstraction. Thus, this is the main difference between package and interface.

Access

Moreover, it is possible to access a package using an import statement while it is possible to extend an interface using another interface or by implementing it using a class.

Keyword

Another difference between package and interface is that the import keyword helps to access a package while the implement keyword helps to access an interface.

Usage

Their respective usage also contributes to a difference between package and interface. That is; a package helps to organize the classes and interfaces to improve maintainability while an interface helps to achieve abstraction and to implement multiple inheritance.

Conclusion

Package and interface are two concepts in programming languages such as Java. The main difference between package and interface is that a package is a collection of related classes and interfaces while an interface is a collection of fields and abstract methods.

Reference:

1. “Java Package – Javatpoint.” Www.javatpoint.com, Available here.
2. “Interface in Java – Javatpoint.” Www.javatpoint.com, Available here.

Image Courtesy:

1. “Dependency inversion” By Kevin Martin – Own work (CC BY-SA 4.0) via Commons Wikimedia

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply