Interfaces

Definition:

In java programming language, an interface is an abstract, conceptual or concrete type that is used to specify an interface.

How to use an interface:

  • By using the key word interface, we can fully abstract a class interface from its implementation.

It means that, using interface, we can specify what a class must do, but not how it does it.

Interfaces are syntactically similar to classes, but they do not instance variables, and their methods are declared without any body. I.e. methods with empty body.

Structure of an interface:

The structure of an interface is much like similar to that of a class.

Below is the general form of an interface:

access-specifier interface InterfaceName { 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; }

Access specifier for an Interface

Access specifier for an interface is either public or it is not used.

Methods for an interface

Notice that, the methods which are declared for an interface will have no bodies.

  • They are, essentially, abstract methods

Variables for an interface

Variables declared inside an interface must possess the following properties:

  • They implicitly final and static, meaning they cannot be changed by the implementing class.
  • They must also be initialized with a constant value

Note: All methods and variables are implicitly public if the interface, itself, is declared as public.

How to implement an interface via class

Once interface 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 the complete set of methods defined by the interface.”

Interfaces are designed in such a way that they support dynamic method resolution at run time.

Interfaces have a different set of hierarchy from classes, thus, 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.

Example of an interface

Below is an example of an interface that declares a simple interface which contains one method called postback( ) and that takes a single integer as a parameter.

interface Postback { void postback(int s); }

Once an interface has been defined, one or more than one class can implement that interface.

For implementing an interface, it includes the implements clause in a class definition, and then creates the methods defined by the interface.

The general form of a class that includes the implements clause looks like this:

access-specifier class classname [extends superclass] [implements interface [,interface...]] { // class-body }
  • Access-specifier is either public or it will not be used.
  • If a class implements more than one interface, the interface names are separated with a comma.
  • If a class implements two interfaces that declare the same method, then the same method will be used by clients of either interface.
  • The methods that implement an interface must be declared public.

Below is an example of a small class that implements the postback interface:

class Client implements postback { // Implement Postback's interface public void callback(int p) { System.out.println("postback called with " + p); } }

Variables in interfaces a more look

We can use interfaces to import shared constants into multiple classes by simply declaring an interface that contains variables which are initialized to the desired values.

When we include that interface in a class (that is, when we “implement” the interface), all of those variable names will be in scope as constants.
If an interface contains no methods, then any class that includes such an interface doesn’t actually implement anything.

Interface can be extended from another interface

If we want to inherit an interface from another we use the keyword extends.
The syntax of extending an interface 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.

Add new comment