An interface in the Java programming language is an abstract type which is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interfacekeyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final).
As interfaces are abstract, they cannot be directly instantiated. Object references in Java may be specified to be of an interface type; in which case they must either be null, or be bound to an object which implements the interface.
The keyword implements is used to declare that a given class implements an interface. A class which implements an interface must either implement all methods in the interface, or be an abstract class.
Interfaces are used to collect like similarities which classes of various types share, but do not necessarily constitute a class relationship. For instance, a human and a parrot can both whistle, however it would not make sense to represent Humans and Parrots as subclasses of a Whistler class, rather they would most likely be subclasses of an Animal class (likely with intermediate classes), but would both implement the Whistler interface.
Another use of interfaces is being able to use an object without knowing its type of class, but rather only that it implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it is a human or a parrot, all that could be determined is that a whistler is whistling. In a more practical example, a sorting algorithm may expect an object of type Comparable. Thus, it knows that the object's type can somehow be sorted, but it is irrelevant what the type of the object is.
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.
Thus, a simple interface may be
public interface Predator {
public boolean chasePrey(Prey p);
public void eatPrey(Prey p);
}
public class Cat implements Predator {
public boolean chasePrey(Prey p) {
// programming to chase prey p
}
public void eatPrey (Prey p) {
// programming to eat prey p
}
}
If a class implements an interface and is not abstract, and does not implement all its methods, this will result in a compiler error. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods.
Classes can implement multiple interfaces
public class Frog implements Predator, Prey { ... }
/*
* In ancient times, there
* were Monsters and Greeks
* who fought each other
*/
public abstract class Creature
{
protected String name;
//...
public void Attack()
{
System.out.println(name + " attacks!");
}
}
public class Achilles extends Creature
{
//...
}
public class Mino extends Creature implements Monster
{
//...
public void Roar()
{
System.out.println(name + " roars loudly!");
}
}
public interface Monster
{
void Roar();
}
public static void main(String[] args)
{
Creature aCreature;
//...
/**
* You have to cast aCreature to type Monster because
* aCreature is an instance of Creature, and not all
* Creatures can Roar. We know it is a Monster,
* therefore it can Roar because we check in the if
* statement below. Both Creatures can attack, so we
* attack regardless...just if it's a monster we'd
* like to roar before attacking.
*/
if(aCreature instanceof Monster)
((Monster)aCreature).Roar(); //It's a monster so we can roar
//Both can attack
aCreature.Attack();
}
Comparable has the method compareTo, which is used to describe two objects as equal, or to indicate one is greater than the other. Generics allow implementing classes to specify which class instances can be compared to them.
Serializable is a marker interface with no methods or fields - it has an empty body. It is used to indicate that a class can be serialized. Its Javadoc describes how it should function, although nothing is programmatically enforced.