1

Is there any way to include the name of the interface when implementing the method? If I have to implement 3 interfaces, then it would be hard to remind me where the implemented method comes from.

If I have 2 interface required to implement the same method name. How can I tell which method I am implementing?

public interface BarInt {
void method();
}
public interface GeeInt{
void method();
}
public class Foo implements BarInt, GeeInt{

@Override
public void method() {
    // TODO Auto-generated method stub

}
}

Thanks

Ming Leung
  • 385
  • 2
  • 13
  • You can always leave a comment. – shmosel Jul 13 '17 at 20:08
  • I know, but is there any java technical way? – Ming Leung Jul 13 '17 at 20:11
  • 1
    Method Names should always *express what the method does*. In which interface a method is declared is a *technical detail* and no useful information for the reader of your code. – Timothy Truckle Jul 13 '17 at 20:12
  • if comments aren't "technical" enough you could use javadoc annotations with the description containing the information you are after – Alex Jul 13 '17 at 20:15
  • @TimothyTruckle: On the contrary, an `@Override(BarInt.class)` or similar annotation would be useful for the same reasons `@Override` is: it helps ensure you're overriding what you think you are, and it helps make it clear why some methods exist, if they're only there to satisfy an interface contract. For example, `add` on an immutable `List`. – user2357112 Jul 13 '17 at 20:18
  • Can I suggest that not being able to remember which interface's method you're implementing might suggest that you're implementing too many interfaces in a single class? Remember: classes should have a [single responsibility](https://en.wikipedia.org/wiki/Single_responsibility_principle); if it's a Foo *and* a Bar *and* a Baz, then it's not really doing that. – Andy Turner Jul 13 '17 at 20:23
  • Normally the IDE's show you from where the method comes and shows you a little sign, that this method comes from an interface... But of course you will only see it in the IDE and not in the code by itself. – Mafick Jul 13 '17 at 20:23
  • If you're implementing `BarInt` and `GeeInt`, which interface is `method()` "from"? Which name do you want to have inserted automatically? – Andy Turner Jul 13 '17 at 20:41

3 Answers3

1

Yes, you can just use a @see javadoc comment

public interface BarInt {
    void method();
}

public class Foo implements BarInt{

    /**
     * @see BarInt#method()
     */    
    @Override 
    public void method() {
        // TODO Auto-generated method stub
    }
}
Novaterata
  • 4,356
  • 3
  • 29
  • 51
  • This isn’t a bad idea, but it’s worth noting that the generated javadoc will already have a link to the interface method which that method is implementing. Notice the **Specified by:** section in the [documentation of String.length()](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--). – VGR Jul 13 '17 at 20:49
  • @VGR sure, but this would have it in the code which was the question. I wouldn't even think to look in the javadoc if I had the source in front of me. Of course I'd just use the IDE to jump to definition, but it's what the OP requested – Novaterata Jul 14 '17 at 00:32
0

Novaterata's answer in good, this is a similar approach with annotations.


You can always roll your own annotations.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Source {

    Class value();

}

Then you can use it like this:

@Source(List.class)
@Override
public boolean add(Object o) {
    return false;
}

With the retention policy of Source the annotation will not be part of the bytecode. It only serves as additional information for the reader, IDE and compiler.

Aron
  • 55
  • 1
  • 7
-2

Design wise name of method would reflect what the method does and you should be able to co-relate with the Interface the method belongs to.

De-bugging wise, most editor will take you the implemented Interface and method on few key strokes/clicks.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
Subir Kumar Sao
  • 8,171
  • 3
  • 26
  • 47