The ActionListener interface has a single method.
void actionPerformed(ActionEvent e) For full Java documentation see Oracle JavaDoc URL in related links below.
1
They key word here is anonymous class. While ActionListener may be an interface, the anonymous class would be a subclass of ActionListener. It would be like creating a new class which implements ActionListener.JButton button = new JButton("Press Me!");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ev) {System.out.println("PRESS");}});
An ActionListener is exactly what it sounds like. It's an interface used by other classes to listen for an action event. The simplest example of this is on a Button. Normally when you add a Button to a Component nothing will happen when you press it. You need to use a button.addActionListener(actionListener) call to make it listen for button clicks.
interface incompatibility means two inherited methods are not Override
Comparison between an Abstract Class and an Interface:While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict:
public
They key word here is anonymous class. While ActionListener may be an interface, the anonymous class would be a subclass of ActionListener. It would be like creating a new class which implements ActionListener.JButton button = new JButton("Press Me!");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent ev) {System.out.println("PRESS");}});
An ActionListener is exactly what it sounds like. It's an interface used by other classes to listen for an action event. The simplest example of this is on a Button. Normally when you add a Button to a Component nothing will happen when you press it. You need to use a button.addActionListener(actionListener) call to make it listen for button clicks.
interface is a list of methods which implements that interface
A tagging interface type in Java is an interface that has not defined methods such as the java.io.Serializable interface.
Interface is collection of abstract methods which has only declaration and no implementation
interface incompatibility means two inherited methods are not Override
Comparison between an Abstract Class and an Interface:While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict:
public
While an abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods. Another way interfaces differ from abstract classes is that interfaces have very little flexibility in how the methods and variables defined in the interface are declared. These rules are strict: • All interface methods are implicitly public and abstract. In other words, you do not need to actually type the public or abstract modifiers in the method declaration, but the method is still always public and abstract. (You can use any kind of modifiers in the Abstract class) • All variables defined in an interface must be public, static, and final-in other words, interfaces can declare only constants, not instance variables. • Interface methods must not be static. • Because interface methods are abstract, they cannot be marked final, strictfp, or native. (More on these modifiers later.) • An interface can extend one or more other interfaces. • An interface cannot extend anything but another interface. • An interface cannot implement another interface or class. • An interface must be declared with the keyword interface. You must remember that all interface methods are public and abstract regardless of what you see in the interface definition.
The idea is that an interface is not a class, it is just a specification of what classes that implement it must contain.
An interface can only have abstract methods or constants in it. A class can have both that and everything else in Java.
An Interface is nothing but a contract as to how a class should behave. It just declares the behavior as empty methods and the implementing class actually writes the code that will determine the behavior. When you implement an interface, you're agreeing to adhere to the contract defined in the interface. That means you're agreeing to provide legal implementations for every method defined in the interface, and that anyone who knows what the interface methods look like can rest assured that they can invoke those methods on an instance of your implementing class. (Thy need not bother much about how you have implemented it. All they bother about is whether a method of the name mentioned in the interface is available or not)