implements interface
Hello,
I use JDK 5.0 with netbeans 4.0 beta 2
I get an error when I implement an interface like in this following example:
public class MyClass extends javax.swing.JPanel implements java.awt.event.KeyListener {
public MyClass () {
}
}
The text message is:
MyClassMyClass is not abstract and does not override abstract method key released in java.awt.event.KeyListener
Thank you for your help
Claude
[468 byte] By [
claude] at [2007-9-30 20:33:35]

An interface defines a contract with implementors of the interface. An interface may only contain public constants and public, abstract methods. An abstract method is a method declaration with no implementation (code) contained within.
If you implement an interface in your class, you *must* exactly duplicate the interface's method signature, including name, any parameters and the return type. Now, you do not necessarily need to actually place any code in the method, but you do at least need to have the method present in your class.
As an example:
public interface Foo {
public void bar()
}
public class FooImpl implements Foo {
final public void bar() {
// optional code here
}
}
- Saish
"My karma ran over your dogma." - Anon
Saish at 2007-7-7 1:23:05 >

when you implement an interface, you MUST override all of it's methods, expect if the class that implements the interface is abstract, so you must override all of the methods of the KeyListener class...
If you don't need to override all the methods of that interface, you can use a more complicated technique, which is Adapters, use the class KeyAdapter that implements the KeyListener, but you should use it with anonymous inner classes like this:
addKeyListener( new KeyAdapter()
{
//put here any method you want from the keyListener, since the KeyAdpater is an Adapter class( that is, it implements the KeyListener and overrides all of its methods as empty methods)...
}
);