FACTOID # 42: English speaking kids are the world's biggest novel readers - but the least enthusiastic comic readers.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

FACTS & STATISTICS    Simple view

  1. Select countries to view: (hold down Control key and click to select several)

     

     

    Compare:

     

     

  1. Select fact or statistic: (* = graphable)

     

     

     

  2. (OPTIONAL) Compare to statistic: (both need to be graphable)

     

     

     

  3. View result as:

     

       
(OR) SEARCH ALL encyclopedia, stats & forums:   

Encyclopedia > Interface (Java)

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 interface keyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final). Java is a programming language originally developed by Sun Microsystems and released in 1995. ... An abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are not also members of some declared subtype. ... An interface defines the communication boundary between two entities, such as a piece of software, a hardware device, or a user. ... In object-oriented programming, a class is a programming language construct that is used to group related instance variables and methods. ... The following are brief definitions of the keywords for the Java programming language. ... Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ...


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. In the C Programming Language, a null pointer is a special pointer which is guaranteed to compare unequal to a pointer to any object or function. ...


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. In object-oriented programming, a class consists of encapsulated instance variables and subprograms, the methods mentioned below. ...


One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than java.lang.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 may implement any number of interfaces. Multiple inheritance refers to a feature of object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. ... 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. ... In computer science, a type system defines how a programming language classifies values and expressions into types, how it can manipulate those types and how they interact. ... In computer science, a superclass is a class from which other classes are derived. ... Multiple inheritance refers to a feature of object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. ...

Contents

Uses

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. Trinomial name Homo sapiens sapiens Linnaeus, 1758 Humans, or human beings, are bipedal primates belonging to the mammalian species Homo sapiens (Latin: wise man or knowing man) in the family Hominidae (the great apes). ... It has been suggested that True parrots be merged into this article or section. ... A whistle is a one-note woodwind instrument which produces sound from a stream of forced air. ...


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. In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive datatype in the discussion of more concrete... In computer science and mathematics, a sorting algorithm is an algorithm that puts elements of a list in a certain order. ...


Usage

Defining an Interface

Interfaces must be defined using the following formula (compare to Java's class definition). In object-oriented programming, a class consists of encapsulated instance variables and subprograms, the methods mentioned below. ...

 [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 behaviours, all methods are implicitly public. Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ...


Thus, a simple interface may be

 public interface Predator { boolean chasePrey(Prey p); void eatPrey(Prey p); } 

Implementing an Interface

The syntax for implementing an interface uses this formula:

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

Classes may implement an interface. For example, In object-oriented programming, a class consists of encapsulated instance variables and subprograms, the methods mentioned below. ...

 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. In object-oriented programming, a class is a programming language construct that is used to group related instance variables and methods. ... In object-oriented programming, a subclass is a class that inherits some properties from its superclass. ...


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. A callback is often back on the level of the original caller. ...

 First a simple link List for the example public class IntLinker { public int start; public IntLinker point; public IntLinker (int start, IntLinker point) { this.start = start; this.point = point; } } Second, the Interface public interface IntMethod { int theMethod(int var); } Third, the final use of the IntMethod (not yet defined, but used) public class IntFinal { IntLinker build (IntMethod var, IntLinker builder){ return new IntLinker ( var.theMethod (builder.start), builder.point); } } Then, the only code written at some later point defining theMethod by implementing IntMethod Thus, a callback public class ImplMethod implements IntMethod{ public int theMethod(int positive) { return Math.abs(positive);} public static void main(String[] args) { IntFinal i = new IntFinal(); IntLinker t = i.build(new ImplMethod(), new IntLinker(-11, new IntLinker(2, null))); System.out.println(t.start); System.out.println(t.point.start); } } 

Creating subinterfaces

Subinterfaces can be created as easily as interfaces, using the same formula are described above. For example

 public interface VenomousPredator extends Predator, Venomous { interface body } 

is legal. Note how it allows multiple inheritance, unlike classes.


Examples

 /* * 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(); } 

Some common Java interfaces are: Java refers to a number of computer software products and specifications from Sun Microsystems (the Javaâ„¢ technology) that together provide a system for developing and deploying cross-platform applications. ...

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

Generic programming is a style of computer programming where algorithms are written in an extended grammar and are made adaptable by specifying variable parts that are then somehow instantiated later by the compiler with respect to the base grammar. ... The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects. ... In computer science, in the context of data storage and transmission, serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link, either in binary form, or in some human-readable text... Javadoc is a computer software tool from Sun Microsystems for generating API documentation into HTML format from Java source code. ...

External links

  • What Is an Interface?
  • Interfaces and Packages

  Results from FactBites:
 
User Interface Tools - Java (1178 words)
This page presents 20 easy questions and answers about Java and Java script.
This 20 questions and answers would help you to understand what all the Java fuss is about and help you to hold your own in conversation about Java.
Resources related to Java UI is under Programming:User Interface.
Java programming language - Wikipedia, the free encyclopedia (3904 words)
Comparing Java and C++, it is possible in C++ to implement similar functionality (for example, a memory management model for specific classes can be designed in C++ to improve speed and lower memory fragmentation considerably), with the possible cost of extra development time and some application complexity.
Java servlets are server-side Java EE components that generate responses to requests from clients.
Java libraries that are the compiled byte codes of source code developed by the JRE implementor to support application development in Java.
  More results at FactBites »


 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments
Please enter the 5-letter protection code

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms.