How do you get a non-abstract method in an interface?

16,529

Solution 1

If you're interested in anonymous inner classes, they work as shown below.

We'll continue with the MouseListener example.

The interface for java.awt.event.MouseListener looks something like this:

public interface  MouseListener {
  void mouseClicked(MouseEvent e);
  void mouseEntered(MouseEvent e);
  void mouseExited(MouseEvent e);
  void mousePressed(MouseEvent e);
  void mouseReleased(MouseEvent e);
}

Somewhere in your app you may want to respond to mouse events, so using an anonymous inner class you could do something like this.

component.addMouseListener(new MouseListener() {
  public void mouseClicked(MouseEvent e){/*implementation goes here...*/}
  public void mouseEntered(MouseEvent e){/*implementation goes here...*/}
  public void mouseExited(MouseEvent e){/*implementation goes here...*/}
  public void mousePressed(MouseEvent e){/*implementation goes here...*/}
  public void mouseReleased(MouseEvent e){/*implementation goes here...*/}
});

What you've done here is created a new class (without name, hence anonymous) that implements the MouseListener interface. You have not, as suggested above, created a non-abstract method on an interface.

You could have also just created a new named class ("named class" means regular old class):

class MyMouseListener implements MouseListener {
  public void mouseClicked(MouseEvent e){/*implementation goes here...*/}
  public void mouseEntered(MouseEvent e){/*implementation goes here...*/}
  public void mouseExited(MouseEvent e){/*implementation goes here...*/}
  public void mousePressed(MouseEvent e){/*implementation goes here...*/}
  public void mouseReleased(MouseEvent e){/*implementation goes here...*/}
}

Then somewhere else you would do component.addMouseListener(new MyMouseListener());

See the difference?

I hope this helps. Good luck.

P.S. - Read up on inheritance, interfaces, inner classes and anonymous inner classes in Java for a deeper understanding.

Solution 2

Interface methods are by definition public and abstract, so you cannot have non-abstract methods in your interface.

Solution 3

After Java 8, you can define non-abstract methods inside an interface using "default" keyword as fallows.

public interface MyIntface {

    void abstract_method(int a, String b);

    default void nonabstract_method(){
        System.out.println("do something");
    }
}

Solution 4

In Java, interface methods are public and abstract by default.

For example:

public interface IPrint{
    public abstract void print();
}

And that is same as:

public interface IPrint{
    void print();
}

So first option is bad practice. Point is that you can't use non-abstract methods inside of interface, because they are abstract by default. But in abstract class you can use non-abstract or abstract methods.

Solution 5

No, it can't be done in Java 6 or 7. It will become possible, in a round-about way, as part of project lambda, which is slated for Java 8. The proposed mechanism is called extension methods.

Share:
16,529
Ben
Author by

Ben

Updated on June 05, 2022

Comments

  • Ben
    Ben almost 2 years

    How does one make a method in an interface that's not abstract? I know that it CAN be done, I just don't know how.

    I'll give an example of my confusion. In Java you can implement java.awt.event.MouseListener, then you must call the method addMouseListener(Object)... passing your class as a parameter, so the MouseListener knows what object to cast from. How is the addMouseListener(Object) method possible?

    Someone said I was confusing the way anonymous classes work with interfaces having non-abstract methods. How would you implement an anonymous class within a interface so the implementer can call its methods? I'm very new and still a 'noob' at OOP.

  • Ben
    Ben over 13 years
    I know that. How does one make a method in an interface that's not abstract? Or can't it be done?
  • mgv
    mgv over 13 years
    You can't. I think you are confusing abstract classes and interfaces.
  • Todd
    Todd over 13 years
    I can't be done. That's what mgv just said. All methods on an interface are public and abstract. That means you cannot make a method that is not abstract.
  • Ben
    Ben over 13 years
    I'll give an example of my confusion. In Java you can implement java.awt.event.MouseListener, then you must call the method addMouseListener(Object)... passing your class as a parameter, so the MouseListener knows what object to cast from. How is the addMouseListener(Object) method possible?
  • Ben
    Ben over 13 years
    See my comment on Matt's answer to see where my confusion comes from :)
  • mgv
    mgv over 13 years
    You are confusing the interface definition with anonymous classes, which allows you to do things like new interface-name () { class-body }. It's just the IMPLEMENTATION of the interface, not the definition.
  • Ben
    Ben over 13 years
    How would one make a 'anonymous class' within an interface and be able to have the interface's implementer call the anonymous class methods?
  • Ben
    Ben over 13 years
    I'll give an example of my confusion. In Java you can implement java.awt.event.MouseListener, then you must call the method addMouseListener(Object)... passing your class as a parameter, so the MouseListener knows what object to cast from. How is the addMouseListener(Object) method possible and how could I do it?
  • Ben
    Ben over 13 years
    So where does the method 'addMouseListener(MouseListener)' come from?... how are you able to access it from the implementing class? I'm obviously not making much sense to people... :(
  • Todd
    Todd over 13 years
    This isn't part of the answer so I'll put it down here. In this particular case the MouseListener interface has a bunch of methods. If you only want to implement a sub-set of the methods you can extend the MouseAdapter. The MouseAdapter is an abstract class with empty implementations for each method. You can extend it anonymously like above and just implement the methods you care about. component.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){/*this is the only method I care about...*/} });
  • Ben
    Ben over 13 years
    The answer was utter brilliance... thanks a bunch! I just wanted to understand how it worked; you provided just that and more!
  • Noich
    Noich almost 10 years
    Hi and welcome to Stack Overflow. Please note that while your answer remains here, the link's content might change or become unavailable. Please edit your post to contain the essential information provided in the link.