Monday 1 October 2012

Uses of Interface in JAVA

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
An interface is not a class. Writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
An interface is similar to a class in the following ways:
  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The bytecode of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
However, an interface is different from a class in several ways, including:
  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.
Usages of Interfaces

Defining an interface

Interfaces are defined with the following syntax (compare to Java's class definition):
[visibility] interface InterfaceName [extends other interfaces] {
constant declarations
abstract method declarations
}
The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behaviors, all methods are implicitly public.
Thus, a simple interface may be
public interface Predator {
boolean chasePrey(Prey p);
void eatPrey(Prey p);
}
The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.[1]
The syntax for implementing an interface uses this formula:
... implements InterfaceName[, another interface, another, ...] ...
Classes may implement an interface. For example,
public class Lion implements Predator {

public boolean chasePrey(Prey p) {
// programming to chase prey p (specifically for a lion)
}

public void eatPrey (Prey p) {
// programming to eat prey p (specifically for a lion)
}
}
If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods. Although if any of the abstract class' subclasses does not implement all interface methods, the subclass itself must be marked again as abstract.
Classes can implement multiple interfaces
 public class Frog implements Predator, Prey { ... }
Interfaces are commonly used in the Java language for callbacks. Java does not allow the passing of methods (procedures) as arguments. Therefore, the practice is to define an interface and use it as the argument and use the method signature knowing that the signature will be later implemented.

Subinterfaces

Interfaces can extend several other interfaces, using the same formula as described below. For example
 public interface VenomousPredator extends Predator, Venomous {
//interface body
}
is legal and defines a subinterface. Note how it allows multiple inheritance, unlike classes. Note also that Predator and Venomous may possibly define or inherit methods with the same signature, say kill(Prey prey). When a class implements VenomousPredator it will implement both methods simultaneously.

No comments:

Post a Comment