Compare the Difference Between Similar Terms

Difference Between

Home / Technology / IT / Programming / Difference Between Package and Interface in Java

Difference Between Package and Interface in Java

June 5, 2018 Posted by Lithmee

The key difference between Package and Interface in Java is that Package helps to categorize the classes methodically to access and maintain them easily while Interface helps to implement multiple inheritances and to achieve abstraction.

Java is one of the most popular programming languages. The main advantage of Java is that it supports Object Oriented Programming. This methodology allows modeling the real world objects in software. A class is a blueprint to create an object. Each object contains data or fields to describe the attributes or the properties and methods to describe behaviors. This article discusses two concepts related to OOP in Java  in Java that are Package and Interface.

CONTENTS

1. Overview and Key Difference
2. What is Package in Java
3. What is Interface in Java
4. Side by Side Comparison – Package vs Interface in Java in Tabular Form
5. Summary

What is Package in Java?

Java provides a large number of classes. Keeping all the classes in a single folder can be difficult because it is hard to access. This can affect the program manageability. Java uses packages to arrange classes. It is similar to a folder. Java API groups classes into different packages according to the functionality. Therefore, each package contains a related set of classes.

Example of Packages in Java

Few example packages are as follows. The java.io package contains the input, output supporting classes. It includes File, PrintStream, BufferInputStream etc. The java.net package contains the networking related classes. Some examples are URL, Socket, ServerSocket. The java.awt package contains all the classes required to build Graphical User Interfaces. Those are few Java API packages.

When the programmer wants to use a certain class in the program, he should import that package. If the programmer wants to use the BufferInputStream class in the java.io package, he should write the import statement as follows.

import java.util.BufferInoutStream;

Below statement will import all the classes in the util package.

import java.util.*;

It is also possible to create user defined packages.

package employee;

public class Employee {

}

According to the above example, the employee is the package name. The Employee class is a part of the employee package. This file saves as Employee.java to the employee package.

Furthermore, it is possible to import a public class from one package to another. Refer the following example.

Difference Between Package and Interface in Java

Figure 01: Class A

Difference Between Package and Interface in Java_Figure 2

Figure 02: Class B

Class A is in package 1, and it contains the public method called display. Class B is in package 2, and it contains the main method. Even though they are in separate packages; class B can create an object of class A by importing package1. After importing package 1, class B has access to the data and methods of class A.

Overall, Package in Java helps to organize the project files. This is very useful when developing large system because it allows storing all the files in methodical way.  In addition to that, the Java API packages allow the programmers to use already existing classes.

What is Interface in Java?

Sometimes the programmer might not know the definition of the method. In this situations, the programmer can only declare the method. An abstract method is a method that does not have a definition. It only has the declaration. When there is at least one abstract method, that class becomes an abstract class. Moreover, the abstract class can contain abstract methods as well as non-abstract methods. The programmer cannot create objects out of abstract classes.

When a class extends an abstract class, the new class should define all the abstract method in the abstract class. In other words, assume that abstract class A has an abstract method called display. Class B extends class A. Then class B should define the method display.

Example of Interface in Java

Assume that both A and B are abstract classes. If class C is extending A and B, that class C has to define the abstract methods of both classes. This is multiple inheritance. Java does not support multiple inheritance. To implement it, the programmer should use interfaces. If A and B are interfaces, then class C can implement them. Refer following example.

Difference Between Package and Interface in Java_Figure 3

Figure 03: Interface A

Difference Between Package and Interface in Java_Figure 4

Figure 04: Interface B

The interface A has the display1 abstract method, and interface B has the display2 abstract method.

Difference Between Package and Interface in Java_Figure 5

Figure 05: Class C

Class C implements both A and B interfaces. Therefore, it should define both methods.

Key Difference Between Package and Interface in Java

Figure 06: Main Method

Now in the main method, it is possible to create an object of C and call both methods. Likewise, interfaces help to implement multiple inheritance in Java.

Other than multiple inheritance, interfaces help to achieve abstraction. It is one major concept in OOP. Abstraction allows to hide the implementation details and show only the functionality to the user. Further, it allows focusing on what the object does instead of how it is done. As an interface consists of abstract methods, it helps to archive abstraction.

What is the Difference Between Package and Interface in Java?

Package is a group of related classes that provide access protection and namespace management. Interface is a reference type similar to class which is a collection of abstract methods. Package helps to categorize the classes methodically to access and maintain them easily. On the other hand, Interface helps to implement multiple inheritances and to achieve abstraction. This is the main difference between Package and Interface in Java. Further, the way to write a package is in lower case letters such as java.util, java.awt. If the name of the interface is Area, then it is written in, interface Area.

Difference Between Package and Interface in Java in Tabular Form

Summary – Package vs Interface in Java

The difference between Package and Interface in Java is that Package helps to categorize the classes methodically to access and maintain them easily while Interface helps to implement multiple inheritances and to achieve abstraction.

Reference:

1.Tutorials Point. “Java Packages.”  Tutorials Point, 24 Mar. 2018. Available here 
2.Tutorials Point. “Java Interfaces.”  Tutorials Point, 24 Mar. 2018. Available here

Related posts:

Difference Between Hashtable and Hashmap Difference Between Implements and Extends Difference Between Debugger and Compiler Difference Between Waterfall Methodology and RUP Difference Between ASP and PHP

Filed Under: Programming

About the Author: Lithmee

Lithmee Mandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

Leave a Reply Cancel reply

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

Request Article

Featured Posts

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and Cold Symptoms

Difference Between Coronavirus and SARS

Difference Between Coronavirus and SARS

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Influenza

Difference Between Coronavirus and Covid 19

Difference Between Coronavirus and Covid 19

You May Like

What is the Difference Between Rabies Vaccine and Immunoglobulin

What is the Difference Between Rabies Vaccine and Immunoglobulin

What is the Difference Between PAC and Sinus Arrhythmia

What is the Difference Between PAC and Sinus Arrhythmia

Difference Between Adverse Selection and Moral Hazard

Difference Between Culture and Civilization

Difference Between Culture and Civilization

What is the Difference Between Chlamydia IgG and IgM

What is the Difference Between Chlamydia IgG and IgM

Latest Posts

  • What is the Difference Between Venlafaxine and Sertraline
  • What is the Difference Between Malate-Aspartate Shuttle and Glycerol-3-Phosphate Shuttle
  • What is the Difference Between Dysphonia and Dysarthria
  • What is the Difference Between Chilblains and Frostbite
  • What is the Difference Between Blood Blister and Melanoma
  • What is the Difference Between Calcitriol and Cholecalciferol
  • Home
  • Vacancies
  • About
  • Request Article
  • Contact Us

Copyright © 2010-2018 Difference Between. All rights reserved. Terms of Use and Privacy Policy: Legal.