Interface (Java)An interface in the Java programming language is an abstract type that is used to declare a behavior that classes must implement. They are similar to protocols. Interfaces are declared using the Interfaces cannot be instantiated, but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an abstract class. Object references in Java may be specified to be of an interface type; in each case, they must either be null, or be bound to an object that implements the interface. One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java must have exactly one base class, the only exception being OverviewInterfaces are used to encode similarities which the 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 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, because all that could be determined is that a whistler is whistling. The call For example: interface Bounceable {
double pi = 3.1415;
void setBounce(); // Note the semicolon
// Interface methods are public, abstract and never final.
// Think of them as prototypes only; no implementations are allowed.
}
An interface:
UsageDefining an interfaceInterfaces are defined with the following syntax (compare to Java's class definition): [visibility] interface InterfaceName [extends other interfaces] { constant declarations abstract method declarations static method declarations } Example: public interface Interface1 extends Interface2; The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the 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.[3] Implementing interfaces in a classThe 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 {
@Override
public boolean chasePrey(Prey p) {
// Programming to chase prey p (specifically for a lion)
}
@Override
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 Classes can implement multiple interfaces: public class Frog implements Predator, Prey { ... }
Interfaces can share common class methods: class Animal implements LikesFood, LikesWater {
boolean likes() { return true; }
}
However a given class cannot implement the same or a similar interface multiple times: class Animal implements Shares<Boolean>, Shares<Integer> ...
// Error: repeated interface
Interfaces are commonly used in the Java language for callbacks,[4] as Java does not allow multiple inheritance of classes, nor does it allow the passing of methods (procedures) as arguments. Therefore, in order to pass a method as a parameter to a target method, current practice is to define and pass a reference to an interface as a means of supplying the signature and address of the parameter method to the target method rather than defining multiple variants of the target method to accommodate each possible calling class. SubinterfacesInterfaces 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. It allows multiple inheritance, unlike classes. ExamplesSome common Java interfaces are:
See alsoCitations
References
External links |