3

I have a class which is implementing an interface, and one of the methods is called onClick. Is there a way to implement the onClick that the interface wants but name it something else? Something like (and I'm making this up):

public void AnyMethodNameIWant() implements Interface1.onClick

Three reasons I'm asking are:

  1. It would be nice to look at an method signature and know that it's coming from an interface
  2. To avoid 'generic' names like onClick that an interface may require me to have
  3. To distinguish between the same method names in many interfaces

Apologies if this is a fundamentally 'bad' question as I am new to Java.

Mendhak
  • 8,194
  • 5
  • 47
  • 64

5 Answers5

6

No, you can't. Interfaces have to be implemented by a method of the same name in Java.

You can use the @Override annotation with interface implementations (as of Java 6) though, which helps to clarify that this is a method which can't just be renamed arbitrarily.

One option for your second issue might be to create an implementation class just for the purpose of forwarding on the call to a more specific method. You might want to do this as a nested or even anonymous class. I'm not sure I'd usually do this though.

EDIT: Having seen the third question - no, if you have two interfaces with the same method signature in Java, you can only provide one implementation :( Oh, and if you've got two interfaces with the same signature but different return types, it's even worse. You could always write a method of Interface1 getInterface1() which returns an instance of an anonymous inner class proxying the Interface1 methods onto the "main" class.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Alright, I'll live with this. :) Thanks for your answers, everyone. I can only mark one as an answer. – Mendhak Oct 01 '11 at 10:49
2

Nope.

The only thing you could do is add a shadow method that implements the interface and calls your method.

public class MyClass implements Interface1 {
    public void AnyMethodNameIWant() { ...; }
    public void onClick() { AnyMethodNameIWant(); }
}
Charles Goodwin
  • 6,402
  • 3
  • 34
  • 63
2

Those two points

  • To avoid 'generic' names like onClick that an interface may require me to have
  • To distinguish between the same method names in many interfaces

are usually solved by using the Adapter Pattern.

interface IFoo {
    void onClick();
    void onChange();
}

class MyImpl {
    void doSomething(){
        // real code for onClick 
    }
    void doSomethingElse(){
        // real code for onChange
    }

    IFoo getFooAdapter(){
        return new IFoo() {
            @Override
            public void onClick() {
                doSomething();
            }

            @Override
            public void onChange() {
                doSomethingElse();
            }
        };
    }
}

Basically you create an intermediate step which forwards all calls to any interface method to the real implementation.

Naming and signatures can vary. You can also offer different adapters for different interfaces if you want (or must if both interfaces have competing method with different behaviour).

There are quite some possibilities how to hand out the adapter instance - creating a new one every time might not be wise in certain circumstances.

Of course this pattern is nothing you implement just for fun or just for minimal and clean code. But it can solve real problems.

A.H.
  • 63,967
  • 15
  • 92
  • 126
1

You can't rename the method, but you could define both methods (onClick and anyMethodNameIWant) and have onClick just simply call your other method.

@Override
public void onClick() {
    anyOtherMethodNameIWant();
}
Nate W.
  • 9,141
  • 6
  • 43
  • 65
  • @Mizuki Check out Shakedown's use of the `@Override` annotation here -- ut shows the method is defined in an interface or super class. This will even give you compiler errors when you rename the method in the interface and forget to do so with all implementations. – Philipp Reichart Oct 01 '11 at 10:39
1

You of course can have additional methods with different names that point to the same implementation to get the naming you desire.

public void interfaceMethodA(){
   // some implementation here
}

public void AnyMethodNameIWant(){
    interfaceMethodA();
}
John B
  • 32,493
  • 6
  • 77
  • 98