# Java: Interface Exercises 2

Name _______________________________

### Example

    // Note:
    //   ActionListener requires defining actionPerformed(...)
    //   MouseMotionListener requires defining mouseMoved(...) and MouseDragged(...).

    public class MyPanel extends JPanel implements ActionListener, MouseMotionListener {
        public void actionPerformed(ActionEvent e) {
            /* Method body */
        }
        public void mouseDragged(MouseEvent me) {
            /* Method body */
        }
        public void mouseMoved(MouseEvent me) {
            /* Method body */
        }
        // Everything else in this class.
    }

    class SomeActionListener implements ActionListener (
       public void actionPerformed(ActionEvent e) {
       }
    }

    class SomeMouseMotionListener implements MouseMotionListener {
        public void mouseMoved(MouseEvent mevt) {
        }
        public void mouseDragged(MouseEvent mevt) {
        }
    }
    . . .
        JPanel              p   = new JPanel();
        MyPanel             myp = new MyPanel();
        ActionListener      al  = new SomeActionListener();
        MouseMotionListener mml = new SomeMouseMotionListener();

### Questions

Which of these statements are ok? If there is a problem, indicate which statement is in error. For each legal statement with a cast, is the cast necessary? If a cast is missing, show what is required?

1. Legal? ________

          al = p;

2. Legal? ________ If legal, cross out unncessary casts.

          al = (ActionListener)p;

3. Legal? ________

          al = myp;

4. Legal? ________ If legal, cross out unncessary casts.

          al = (ActionListener)myp;

5. Legal? ________

          p = al;

6. Legal? ________ If legal, cross out unncessary casts.

           p = (JPanel)al;

7. Legal? ________

          mml = myp;

8. Legal? ________ If legal, cross out unncessary casts.

          al = myp;
          p = (JPanel)al;

9. Legal? ________

          mml = myp;
          al  = mml;

10. Legal? ________ If legal, cross out unncessary casts.

           mml = (MouseMotionListener)myp;
           al  = (ActionListener)mml;

11. Legal? ________ If legal, cross out unncessary casts.

           al = (ActionListener)myp;
           p = (JPanel)(ActionListener)(MyPanel)(MouseMotionListener)al;