0

Can a Java interface exist that would be implemented by a method rather than by a class?

My professor asked whether Java interfaces can exist for methods, and the students pretty much unanimously said no. The professor told us we need to review interfaces.

I know that interfaces can outline methods, but despite much searching, I can find no documentation of any feature which allows writing a method interface, nor have I ever encountered such a feature in all my years of experience with Java. Moreover, it doesn’t make syntactical or structural sense to me. However, rather than dismiss my professor’s implications, I figured I should ask around a bit.

Gregory Gan
  • 105
  • 11
  • Not sure i got your question, but seems you are asking about default methods (https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html) feature of Java that got introduced since version 8. – Madhusudana Reddy Sunnapu Feb 07 '16 at 02:00
  • He may have been trying to make a point about functional interfaces. Even so, functional interfaces still specify the contract for a family of types, just as ordinary interfaces do. That "functional interfaces are implemented by a method rather than a class" may have been his point, but it doesn't quite ring true for me. – scottb Feb 08 '16 at 02:07

4 Answers4

1

Can a Java interface exist that would be implemented by a method rather than by a class?

Your problem statement is not clear.

Are you looking for an example where a class does not implement an interface and a method in that class provides implementation?

One example:

executor.submit(new Runnable(){
                 public void run(){
                    System.out.println("Thread Name in Runnable:"+
                                        Thread.currentThread().getName());
                 }
            });

In this example,

My main class does not explicitly quote that it implements Runnable interface but it simply implemented run() method of Runnable interface.

EDIT:

If your professor is talking about new features of interface in java 8, have a look at this oracle documentation page. With this new feature of default methods, interface have body in methods i.e. they provide default implementation for some methods.

Some important notes on default methods:

Extending Interfaces That Contain Default Methods

When you extend an interface that contains a default method, you can do the following:

  1. Not mention the default method at all, which lets your extended interface inherit the default method.
  2. Redeclare the default method, which makes it abstract.
  3. Redefine the default method, which overrides it.

Especially have a look at "Extending Interfaces That Contain Default Methods" in oracle documentation page and this article

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
  • I think it’s more likely my professor meant default methods than implementing a method from an interface, because she had already asked about the latter. However, she also said that we need to know Java 7 for the class, which implies that knowledge of features of Java 8 is optional. – Gregory Gan Feb 08 '16 at 15:16
  • I have updated my answer with few more points on default methods. But default methods are introduced in java 8 only. – Ravindra babu Feb 08 '16 at 15:32
0

Interfaces specify methods, and in Java 8 can provide default implementations. I suspect this is a matter of how you have interpreted and paraphrased your instructor's words. Maybe he was referring to the new Java 8 "functional interface" feature. You should research this.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

This sounds like either your professors words where chosen poorly or you misunderstood some of the things he said. I can imagine he talked about anonymous classes, where you can write in line implementations for interfaces like:

ActionListener listener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
       // do stuff
    }
}

Either that or he referred to default implementations for interfaces. It might make sense to ask him to clarify in the next lesson.

Neuron
  • 5,141
  • 5
  • 38
  • 59
0

I did what should have been the obvious thing: I asked my professor for clarification. It seems I read too much into her words; when she said we should go review interfaces, she just meant literally that. She never actually said we were wrong; it seems that, strictly speaking, there are no interfaces for methods. But I learned a lot from all these answers — thanks.

Gregory Gan
  • 105
  • 11