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

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.

2

8 Answers

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

4

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.

2

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"); } } 

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.

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.

3

In Java you just can't. Perhaps what you are thinking about is an abstract class.

An abstract class in Java may implement interfaces and define some method signatures while keeping other methods abstract with the "abstract" keyword -- Wikipedia

So, like in a regular class, you can provide implementation to some concrete methods. And, like in an interface, you can declare the signature of abstract methods. Theses methods will be implemented by the concrete classes that extend the abstract one. Like an interface an abstract class cannot be instantiated.

The main practical difference is that concrete classes are created by sub-classing the abstract one. So one concrete class can only extend one abstract class while it could implement many interfaces.

1

You're confusing "non-abstract method in interface" with "class implementing an interface". Your question:

How is the addMouseListener(Object) method possible?

It's possible because the object you pass to addMouseListener(MouseListener) (which doesn't take an Object, btw, but an instance of the MouseListener interface) is an instance of a class that implements that interface:

public MyMouseListener implements MouseListener { ... } addMouseListener(new MyMouseListener()); 

I'd recommend you read a tutorial on Java (or OO) basics, such as this section of the Java Tutorial.

1

With Java 8 you can have default implementation for methods inside a interface please visit this link.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like