Published by: sadikshya
Published date: 17 Jun 2021
Interfaces and Packages are syntactically similar to classes, but they lack instance variables, and their methods are declared without anybody. A package is a group of classes and interfaces together whereas, an interface is a group of abstract methods. 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. The Interfaces and packages are described below:
Interfaces
Using the keyword interface, you can fully abstract a class’ interface from its implementation. That is, user interface, you can specify what a class must do, but not how it does it. Interfaces are syntactically similar to classes, but they lack instance variables, and their methods are
declared without anybody. In practice, this means that you can define interfaces that don’t make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Also, one class can implement any number of interfaces.
To implement an interface, a class must create a complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism.
Interfaces are designed to support dynamic method resolution at run time. Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are
compatible. This requirement by itself makes for a static and non-extensible classing environment. Inevitably in a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be available to more and more subclasses.
Interfaces are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inheritance hierarchy. Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface. This is where the real power of interfaces is realized.
An interface is defined much like a class. This is the general form of an interface:
access interface name {
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// …
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
When no access specifier is included, then default access results, and the interface is only available to other members of the package in which it is declared. When it is declared as public, the interface can be used by any other code. In this case, the interface must be the only public interface declared in the file, and the file must have the same name as the interface. name is the name of the interface and can be any valid identifier. Notice that the methods that are declared have nobodies. They end with a semicolon after the parameter list. They are,
essentially, abstract methods; there can be no default implementation of any method specified within an interface. Each class that includes an interface must implement all of the methods. Variables can be declared inside of interface declarations. They are implicitly final and
static, meaning they cannot be changed by the implementing class. They must also be initialized. All methods and variables are implicitly public.
Applying Interfaces
To understand the power of interfaces, let’s look at a more practical example. In earlier chapters, you developed a class called Stack that implemented a simple fixed-size stack. However, there are many ways to implement a stack. For example, the stack can be of a fixed size or it can be “growable.” The stack can also be held in an array, a linked list, a binary tree, and so on. No matter how the stack is implemented, the interface to the stack remains the same. That is, the methods push( ) and pop( ) define the interface to the stack independently of the details of the implementation. Because the interface to a stack is separate from its implementation, it is easy to define a stack interface, leaving it to each implementation to define the specifics.
Interfaces Can Be Extended
One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance
chain.
It is repeatedly said that one of the main features of the Object-Oriented Programming is the ability to reuse the code already written by the programmer. One way of achieving is by extending class and using the interface. It has a limitation. What will you have to do if you have to use a class from another program without physically copying them into the program at the time of development? So another way of achieving the concept of reusability in Java is via the use of Packages. In this chapter, you will learn about how packages are used within a Java program.
Packages in Java are groups of similar types of classes, interface, and sub-packages. It is a way of grouping a variety of classes or interfaces collectively. The grouping is usually done according to functionality. The Java packages act as containers for Java classes.
There is also a term named sub-packages. The package inside the package is called the sub-package. It should be created to categorize the package further.
While creating a package, programmers must choose a name for the package and include a package statement along with that name at the top of the source program that contains the classes, interfaces, enumerations, and annotation types that you want to include in the package. There can be only one statement of the package in each source code, and it applies to all types in the file.