What are packages and interfaces in java

Difference Between Package and Interface in Java

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

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.

Читайте также:  Зачем нужен static php

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.

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

It is also possible to create user defined packages.

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

Источник

Difference Between Packages and Interfaces in Java

packages-and-interfaces

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.

  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

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

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.

Comments

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

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

Источник

Оцените статью