I have looked up interfaces. Are interfaces similar to classes except they they only contain empty methods?
Printable View
I have looked up interfaces. Are interfaces similar to classes except they they only contain empty methods?
Just as some applications require a user to intact with it, using a UI; some require interaction with other applications, systems and processes. This is done via an API. in java, an interface defines the entry point making it generic.
This means, no matter who writes the entry point, as long as it implements the interface, it will always have the required signature. This allows multiple developers to work on integrations independently while keeping compatibility.
WTF?
Your entire post confused the cr@p out of me!
Another way to see interfaces is as a way to give a data type to a class. Java requires most arguments to methods to be of a certain data type. Interfaces allow one class to have many different data types and therefore can be passed as arguments to the methods that require that datatype.
Most interfaces define a method that the class that implements the interface must define with code.
Look at the ActionListener interface. It has one method you fill in for listening for button presses for example. The class that implements that interface can pass itself via the this variable to the addActionListener which requires a ActionListener for its argument.
More or less, a system which you are interacting with is expecting a square. If you have a number of different developers, say, Bob, John and Sam, who want to interact with this system, they will all write different code. Bob writes a triangle, John writes a hexagon and Sam writes a dodecahedron. The square from the system won't fit into any of these.
An interface forces the developers to build the square. They can add extra functionality, but their entry point will be a square.
Ahh I get what your saying.
So when I do "fileItem.addActionListener(new ActionListener(){});", I am adding a method named "actionPerformed" to the fileItem object?
No. Your method of creating an anonymous class is incorrect.
You need to add the method definition inside of the {}s
Do a Search on the Forum for ActionListener for sample code.
I know that :), i couldnt be bothered to write it all out. So by writing:
"fileItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
code....
}
});",
... i am adding the method actionPerformed to the fileItem object, and whenever an action occurs on this object the actionPerformed method is called?
Not quite. You are adding an instance of an ActionListener object to the fileItem class's list of ActionListeners.Quote:
i am adding the method actionPerformed to the fileItem object,
YesQuote:
whenever an action occurs on this object the actionPerformed method is called
As an ActionListener is an interface, how can i add an instance of this? Because interfaces cant be instantiated can they.
The compiler will create an anonymous (no name) class the way that you coded it in post#9.
Oh yeah :)