Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Why must declare all methods for abstract class.

  1. #1
    hqt
    hqt is offline Member
    Join Date
    Nov 2011
    Posts
    27
    Rep Power
    0

    Question Why must declare all methods for abstract class.

    I want to add KeyboardPressed Event for my program. So here is my code:
    Java Code:
    public class Example extends Applet implements KeyListener{
        public void init(){
             addKeyListener();
        }
    /*Key Handle*/
        public void keyReleased(KeyEvent k){}
        public void keyTyped(KeyEvent k){}
        public void keyPressed(KeyEvent k){
            //Some code here 
        } 
    }
    My question is: unlike when I program on .NET, in Java, when you want to add a KeyboardEvent, you must declare all of its method. (keyReleased, keyTyped, keyPressed) although I just want keyPressed do some works for me.
    maybe this make me hard to remember all (if I don't use Java IDE that can auto generate code for me)

    thanks :)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Why must declare all methods for abstract class.

    First, your topic title is wrong. The methods are declared in the interface (not Abstract class) and are required to be implemented in the concrete class.

    Implementing an interface in a class is often a bad design choice. In your example, the Example class is-a-Applet, but there's no reason it should be a KeyListener. Rather, it uses a KeyListener for its internal workings.

    The single line of code in your init() method is also wrong and won't compile. There's no Applet method addKeyListener() without a parameter.

    I would use a KeyListener declared as a field, local variable or directly in the add... method, extending KeyAdapter -- which has empty implementations of all KeyListener methods. For the last, the code would look something like
    Java Code:
        public void init(){
            addKeyListener(new KeyAdapter() {
    
                public void keyPressed(KeyEvent k){
                    //Some code here 
                } 
            });
        }
    That said, for listening for keystrokes in an Applet, you'd be better off using Swing (JApplet, not Applet, with all Swing components added) and Key Bindings.

    db
    hqt likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 3
    Last Post: 11-22-2011, 05:51 AM
  2. Replies: 2
    Last Post: 09-27-2011, 03:59 PM
  3. How can I call abstract class methods from another class
    By srinivas2828 in forum New To Java
    Replies: 13
    Last Post: 03-12-2010, 02:33 PM
  4. Declare methods in a class
    By Adiel224 in forum New To Java
    Replies: 5
    Last Post: 09-19-2008, 10:38 AM
  5. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •