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:
1 2 3 4 5 6 7 | public interface IMovable { void crawl(); void run(); void jump(); } |
In the above example, IMovable is the interface name which contains three methods crawl, run 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | abstract class Animal implements IMovable { abstract void area(); } class Person implements IMovable { @Override public void crawl() { System.out.println("Person is crawling." "); } @Override public void run() { System.out.println("Person is running."); } @Override public void jump() { System.out.println("Person is jumping."); } } public class Driver { public static void main(String[] args) { Person ramesh = new Person(); IMovable obj = ramesh; obj.crawl(); obj.run(); obj.jump(); } } |
In the above example, Person class provides implementation for methods in the interface IMovable.
Take your time to comment on this article.