Java

How to use interfaces in your java code

An interface is a collection of method prototypes (method name followed by parameters list without any body). The syntax of a method prototype is as follows:

return-type  method-name(parameters-list);

An interface can contain only constants and method prototypes. The use of an interface is to abstract the class’ behavior from its definition. In this way an interface can specify a set of method prototypes which can be implemented by one or more classes.




Differences between interface and a class

  • Objects can be created for classes, where as it is not possible for interfaces.
  • Classes can contain methods with body, where as it is not possible in interfaces.
  • Classes can contain variables, where as it is not possible in interfaces.
  • Some classes can be final, where as interfaces cannot be declared as final.
  • Some classes can be abstract, where as interfaces cannot be declared as abstract.
  • Various access specifiers like public or private or default can be applied to classes, where as only public or default access specifier is applicable for top-level interface.

Defining an Interface

The definition of an interface is very much similar to the definition of a class. The syntax for defining an interface is as follows:

interface  interface-name

{

return-type  method1(parameters-list);

return-type  method2(parameters-list);

data-type  variable-name1 = value;

data-type  variable-name2 = value;

}

Access specifier before interface keyword can be public or default (no specifier). All the methods inside an interface definition does not contain any body. They end with a semi-colon after the parameters list. All variables declared inside an interface are by default final and static. All methods declared inside an interface are by default abstract and both variables as well as methods are implicitly public. An example for Java interface is as follows:

In the above example, IMovable is the interface name which contains three methods crawlrun and jump.

Implementing Interfaces

The methods declared in an interface definition must be implemented by the class which inherits that interface. This process of a class implementing the methods in an interface is known as implementing interfaces.

It is mandatory for a class to implement (provide body) all the methods available in an interface. Otherwise, if a class provides implementation for only some methods (partial implementation) then the class should be made abstract. The methods of the interface must be declared as public in the class. Syntax for implementing an interface is as follows:

class ClassName implements InterfaceName

{

//Implementations of methods in the interface

}

Let’s consider two classes Animal and Person which implements the interface IMovable defined above:

In the above example, Person class provides implementation for methods in the interface IMovable.

Take your time to comment on this article.

Leave a Comment