Java Interfaces?

Creedyou

Senior member
Dec 28, 2001
205
0
0
So I finally got my Java book and I have come across a topic I don't fully understand, interfaces. What are they and what are they used for?
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
Kind of an important concept to grasp when working in Java.
An interface is a "class" with no members and all abstract methods.
All classes that implement the interface are considered an instance of the interface and are therefore polymorphic. They are used very often in the javax.swing package to implement the listener concept.
Other than that I suggest reading a book or one of the many tutorials on the java.sun.com website. This concept is just too important to not understand completely.
 

manly

Lifer
Jan 25, 2000
13,083
3,849
136
An interface is not a "class". That interpretation probably comes from a C++ viewpoint, whereas the closest thing to a Java Interface is a pure abstract class.

Instead of thinking in terms of abstract methods or classes, just understand that an interface defines a set of operations (or behaviors). Interface is a language construct whereby you bundle up related operations into one name.

While in Java, polymorphism and OOP relate to the actual use of Interfaces, interface is an even more general programming concept that spans various language paradigms. The main importance is to separate what a "thing" does (the interface) from how it actually does it internally (the implementation). Effectively separating the two is crucial for streamlining maintenance.