-3

How many methods is an ideal amount for an interface to have? And how can you tell how many methods an interface have from an implementation? Would the Mouselistener interface have 5 methods then?:

   // ToggleButton Listener Class:
    class ToggleButton implements MouseListener {

            public void mousePressed(MouseEvent e) { }

            public void mouseReleased(MouseEvent e) { }

            public void mouseEntered(MouseEvent e) { }

            public void mouseExited(MouseEvent e) { }

            public void mouseClicked(MouseEvent e) {
               // e.getButton() returns 0, 1, 2, or 3, where 1 is the
               // left mouse button and 3 is the right mouse button:
               mousebutton = e.getButton();

               // Identify which JButton was clicked on by getting the
               // source of the event e; Book, p. 484 (Event and Event
               // Source);
               // e.getSource() returns an object of the Object
               // superclass, and that object has to be cast to a
               // JButton with (JButton):
               JButton B = (JButton)e.getSource();
               nextSymbol( B );
            }

    } // end ToggleButton class
Sophia Ali
  • 301
  • 2
  • 6
  • 14
  • 1
    All these questions are irrelevant in practice. What exactly is your use case? – kolossus Jul 09 '13 at 00:55
  • @kolossus it is an extra credit assignment to find out how many methods the Mouselistener interface has. I think it has 5 but I am not sure, and I was just curious if an interface can only have a certain number of methods and how to actaully tell how many methods an interface has – Sophia Ali Jul 09 '13 at 00:57
  • one for each getter and setter, and one for each action. or 7 - but sometimes 42. – Randy Jul 09 '13 at 01:10

3 Answers3

0

Well, you could start by going to the documentation and counting the number of methods there, taking care to grab any from its super interface.

How many methods is an ideal amount for an interface to have?

As many as it needs, no more nor more less. Sounds flip, but it's not: the answer really is it depends. Some interfaces define none (yes, none; cf. EventListener) or one method, others define many methods. And both can make sense.

And how can you tell how many methods an interface have from an implementation?

You can't because some of the methods defined in a class that implements an interface might be specific to the class and not on the interface.

Would the Mouselistener interface have 5 methods then?

Looking at the docs, yes.

jason
  • 236,483
  • 35
  • 423
  • 525
0

There's no ideal number. It depends on the interface in question.

For instance, Runnable has one important method, void run(), while List has [quite a few[(http://docs.oracle.com/javase/6/docs/api/java/util/List.html). Serializable even has none and only acts as a semantic marker in the spirit of marker interfaces.

The former is a light wrapper for a method given dynamically as an object, while the latter is a quite extensive contract of what lists should be able to do.

All you need to do is declare the methods you need, and ignore the ones you don't.

You can always extend when creating your own. For instance, BasicClickListener could listen for clicks, while ExtendedClockListener which extends BasicClickListener could also handle dragging and mouseovers.

The official MouseListener as five.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

You can retrieve the number of methods on an interface (or class) with a little reflection. ToggleButton can reflectively get the number of classes available by defining the following method

        public void countInterfaceMethods(){
          //this will get an array of all the interfaces your class implements
          Class<?>[] clazz =    this.getClass().getInterfaces(); 
          //retrieve only the first element of the array, because ToggleButton implements only one interface
          Class<?> theFirstAndOnlyInterface = clazz[0]; 
          //get all the methods on that interface
          Method[] theArrayOfMethods = theFirstAndOnlyInterface .getMethods(); 
          //get the length of the array. This is the number of methods in that interface
          int numberOfMethods = theArrayOfMethods.length;

        }

Technically, as you can see, it's feasible. But just because something is feasible doesn't mean it should be done

kolossus
  • 20,559
  • 3
  • 52
  • 104