1

Possible Duplicate:
What is an interface in Java?

Im confused by this term. I search google and this is what I found.

An interface is the place where two different things meet and interact. This term often comes up with regard to computers. Data processing takes place inside the computer, and thoughts take place inside the user of the computer, and they meet at an interface, which is a keyborad and a monitor screen (and usually speakers as well). [DATA -> INTERFACE <- USER]

But in this site I found this.

Java contains many libraries in those packages (Swing, etc.), and the API is the interface by which we request services (perform actions, etc.).[PACKAGES->API<-PROGRAMMER]

But in java we use it like this...
public interface A
public class B implements A
We use it to implement methods from A. Interface here is not a connection between B and methods().

Community
  • 1
  • 1
user1293258
  • 109
  • 8

1 Answers1

0

The everyday definition of "interface" seems far removed from the technical meaning of interface in Java, but the two definitions are easily related. In Java, an interface is the specification of the place where an object and other code interact. As with the use of "interface" more generally, a Java object can be treated as having more than one interface.

The critical difference between a Java interface and a Java class is that an interface is purely a specification, whereas a class is a specification plus an implementation of one side of the interface (the object side). (There's an exception: if a class is declared abstract, methods can also be declared abstract, in which case their implementation is defined by subclasses.) A Java class can be declared to implement one or more interfaces, which means that the class (if it is not abstract) must include an implementation for each method specified in each interface.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • But the interface hold the code, unlike keyboard holds the data. – user1293258 Apr 03 '12 at 03:03
  • @user1293258 - The usual purpose of code in a procedural language like Java is to specify behavior--a procedure for the computer to execute. In that sense, a Java interface does not hold any code. (Yes, an interface is specified in code, but it does not contain any code that defines behavior.) – Ted Hopp Apr 03 '12 at 03:24
  • But interface in java is not a connection between code and the programmer, unlike the term interface which means connection between data and the user. – user1293258 Apr 03 '12 at 03:41
  • @user1293258 - A Java interface is more abstract: it's a connection between an object that implements the interface and code that interacts with the object. The programmer is not on either side of the interface. – Ted Hopp Apr 03 '12 at 03:44
  • but the code that interacts with the object is declared in the inteface itself. **Object->Interface<-Code** – user1293258 Apr 03 '12 at 03:59
  • @user1293258 - You are misinterpreting what I've written. Consider a single method in a class. There are two kinds of code: code that defines how a method is invoked (and what type it returns) and code that defines the behavior of the method. In C/C++, the former is called the declaration and the latter the definition. In Java, they are called the method signature and the method body. The latter kind of code (behavior/definition/body/whatever) never appears in an interface. An interface only contains code that defines the meeting place between an object and code that uses the object. – Ted Hopp Apr 03 '12 at 04:12