Interface (Java)
Encyclopedia
An interface in the Java programming language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 is an abstract type
Abstract type
In programming languages, an abstract type is a type in a nominative type system which cannot be instantiated. An abstract type may have no implementation, or an incomplete implementation...

 that is used to specify an interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

 (in the generic sense of the term) that classes
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

 must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations (variable declarations that are declared to be both static and final
Final (Java)
In the Java programming language, the final keyword is used in several different contexts to define an entity which cannot later be changed.- Final classes :...

). An interface may never contain method definitions.

Interfaces cannot be instantiated. A class that implements an interface must implement all of the methods described in the interface, or be an abstract class. 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 that implements the interface.

One benefit of using interfaces is that they simulate multiple inheritance
Multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....

. All classes in Java must have exactly one base class, the only exception being , the root class
Top type
The top type in type theory, commonly abbreviated as top or by the down tack symbol , is the universal type—that type which contains every possible object in the type system of interest. The top type is sometimes called the universal supertype as all other types in any given type system are...

 of the Java type system
Type system
A type system associates a type with each computed value. By examining the flow of these values, a type system attempts to ensure or prove that no type errors can occur...

); multiple inheritance
Multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....

 of classes is not allowed. A Java class may implement, and an interface may extend, any number of interfaces; however an interface may not implement an interface.

Overview

Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. For instance, a human
Human
Humans are the only living species in the Homo genus...

 and a parrot
Parrot
Parrots, also known as psittacines , are birds of the roughly 372 species in 86 genera that make up the order Psittaciformes, found in most tropical and subtropical regions. The order is subdivided into three families: the Psittacidae , the Cacatuidae and the Strigopidae...

 can both whistle
Whistle
A whistle or call is a simple aerophone, an instrument which produces sound from a stream of forced air. It may be mouth-operated, or powered by air pressure, steam, or other means...

; 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 both would implement the Whistler interface.

Another use of interfaces is being able to use an object
Object (computer science)
In computer science, an object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure...

 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. In a more practical example, a sorting algorithm
Sorting algorithm
In computer science, a sorting algorithm is an algorithm that puts elements of a list in a certain order. The most-used orders are numerical order and lexicographical order...

 may expect an object of type . Thus, it knows that the object's type can somehow be sorted, but it is irrelevant what the type of the object is. The call whistler.whistle will call the implemented method whistle of object whistler no matter what class it has, provided it implements Whistler.

For example:


interface Bounceable
{
void setBounce; // Note the semicolon:
// Interface methods are public and abstract.
// Think of them as prototypes only; no implementations are allowed.
}

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
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...

, 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.
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.

Classes can implement multiple interfaces

public class Frog implements Predator, Prey { ... }


Interfaces are commonly used in the Java language for callbacks
Callback (computer science)
In computer programming, a callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine defined in a higher-level layer....

. 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.

Examples

Some common Java
Java (Sun)
Java refers to several computer software products and specifications from Sun Microsystems, a subsidiary of Oracle Corporation, that together provide a system for developing application software and deploying it in a cross-platform environment...

 interfaces are: has the method , which is used to describe two objects as equal, or to indicate one is greater than the other. Generics
Generic programming
In a broad definition, generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters...

 allow implementing classes to specify which class instances can be compared to them. 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
Serialization
In computer science, in the context of data storage and transmission, serialization is the process of converting a data structure or object state into a format that can be stored and "resurrected" later in the same or another computer environment...

. Its Javadoc
Javadoc
Javadoc is a documentation generator from Sun Microsystems for generating API documentation in HTML format from Java source code.The "doc comments" format used by Javadoc is the de facto industry standard for documenting Java classes. Some IDEs, such as Netbeans and Eclipse automatically generate...

describes how it should function, although nothing is programmatically enforced.

Java interface modifiers

Access Modifier Interface Modifier
public abstract
strictfp


Note that improper usage of modifiers of interfaces may result in unstable software behavior.

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK