I have looked up interfaces. Are interfaces similar to classes except they they only contain empty methods?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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!
Improving the world one idiot at a time!
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.
Last edited by Norm; December 7th, 2011 at 05:26 PM.
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.i am adding the method actionPerformed to the fileItem object,
Yeswhenever 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
Interfaces are similar to class, known as reference type which provides polymorphism to a great extent, as they can only contain signature of abstract methods, which can be implemented by different classes as per the user requirement.
They are the only way through which multiple inheritance cane be implemented in java.