Interface (Java)

Interface (Java)

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final). An interface may never contain method definitions.

As interfaces are implicitly "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".

One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than Javadoc:SE|package=java. _ja. Object, the root class of the Java type system) must have exactly one base class; multiple inheritance of classes is not allowed. However, a Java class/interface may implement/extend any number of interfaces.

Overview

Interfaces are used to encode 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 both would 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, because 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 Javadoc:SE|java/lang|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 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();/*Interface methods are by default public and abstract and the methods in an interface ends with a semicolon not with curly brace.*/ }

Usage

Defining an interface

Interfaces are defined with the following syntax (compare to Java's class definition).

["visibility"] interface "InterfaceName" [extends "other interfaces"] { "constant declarations" "member type 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 behaviours, all methods are implicitly public.

Thus, a simple interface may bepublic interface Predator { boolean chasePrey(Prey p); void eatPrey(Prey p);}

The member type declarations in an interface are implicitly static and public, but otherwise they can be any type of class or interface. [cite web|url=http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.5|title=The Java Language Specification] "'

The syntax for implementing an interface uses this formula:

... implements "InterfaceName" [, "another interface", "another", ...] ...

Classes may implement an interface. For example, public class Cat implements Predator {

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

public void eatPrey (Prey p) { // programming to eat prey p (specifically for a cat) 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. [cite web|url=http://www.javaworld.com/javaworld/javatips/jw-javatip10.html|title=Java World] 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.

ubinterfaces

Interfaces can extend several other interfaces, using the same formula are described above. 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 interfaces are:
* has the method Javadoc:SE|name=compareTo|java/lang|Comparable|compareTo(T), 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.
* 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.

Java interface modifiers

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

References

External links

* [http://java.sun.com/docs/books/tutorial/java/concepts/interface.html What Is an Interface?]
* [http://java.sun.com/docs/books/tutorial/java/interpack/index.html Interfaces and Packages]
* [http://javapapers.com/java-interview-questions/abstract-and-interface-java-interview-questions/difference-between-a-java-interface-and-a-java-abstract-class/ Difference between Interface and Abstract Class]


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Interface (Java) — …   Википедия

  • Java (Plattform) — Die Java Plattform (englisch Java Platform) definiert die Ablaufumgebung (Java Virtual Machine) und Programmierschnittstellen (Java Application Programming Interface) innerhalb der Java Technologie. Der Kern der Java Plattform ist die Java… …   Deutsch Wikipedia

  • Java Platform, Standard Edition 6 — Die Java Plattform (englisch Java Platform) definiert die Ablaufumgebung (Java Virtual Machine) und Programmierschnittstellen (Java Application Programming Interface) innerhalb der Java Technologie. Der Kern der Java Plattform ist die Java… …   Deutsch Wikipedia

  • Java Plattform — Die Java Plattform (englisch Java Platform) definiert die Ablaufumgebung (Java Virtual Machine) und Programmierschnittstellen (Java Application Programming Interface) innerhalb der Java Technologie. Der Kern der Java Plattform ist die Java… …   Deutsch Wikipedia

  • Interface — may refer to:in computer science: * Interface (computer science), an abstraction of a software component. * Interface (Java), an abstract type which is used to specify an interface that classes must implement * Network interface, a point of… …   Wikipedia

  • Java Native Interface — JNI redirects here. For the city in the province of Buenos Aires, see Junín, Buenos Aires. The Java Native Interface (JNI) is a programming framework that enables Java code running in a Java Virtual Machine (JVM) to call and to be called[1] by… …   Wikipedia

  • Java Native Interface — Le JNI (Java Native Interface) est un framework qui permet au code Java s exécutant à l intérieur de la JVM d appeler et d être appelé[1] par des applications natives (c est à dire des programmes spécifiques au matériel et au système d… …   Wikipédia en Français

  • Java (значения) — …   Википедия

  • Java Platform, Standard Edition — or Java SE is a widely used platform for programming in the Java language. It is the Java Platform used to deploy portable applications for general use.In practical terms, Java SE consists of a virtual machine, which must be used to run Java… …   Wikipedia

  • Java (informatique) — Java (langage) Pour les articles homonymes, voir Java.  Ne doit pas être confondu avec JavaScript …   Wikipédia en Français

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”