user14610044
user14610044

Reputation:

Why shouldn't i change the interface method names when implementing them?

Say I'm using a Runnable interface,

Runnable runnable = new Runnable() {
    @Override
    public void run() {
        // some code

    }
};

If the run () method in Runnable interface is empty , why should we use @Override and same name for the method in our code instead of just using a random name for our implementation like ,

Runnable runnable = new Runnable() {
    public void myRun() {
        // some code

    }
};

The question might seem dumb but I'm a newbie to programming!

Upvotes: 3

Views: 298

Answers (3)

Dishonered
Dishonered

Reputation: 8851

As to why an interface implementation method has to have the same name as the declaration method name in the interface , i think it boils down to allowing Java compiler to understand which method actually implements the interface. Otherwise its impossible for the Java compiler to know. Another solution could have been to use a qualifier name in an annotation to signal which is the implementation method something like this.

public interface MyInterface {
    void sayHello();
}

and

public class Hello implements MyInterface {

   @Implements("MyInterface.sayHello")
   public void someOtherName() {
       print("Hello World);
   }

}

This approach is not valid , but could have been an alternative if Java creators wanted to allow users to declare different names for interface implementation methods. I think this approach is uglier and prone to errors though.

Also this problem does not exist when using lambda expressions where the compiler can understand whats happening without the code writer having to specify the name of the interface method. Note that this only applies to functional interfaces.

Upvotes: 1

Sweeper
Sweeper

Reputation: 273540

Think about what it means to "implement an interface". If I have a variable of an interface type, I can call any method defined in the interface on that variable. For example, for the Runnable interface:

interface Runnable {
    void run();
}

If I have a variable of type Runnable, I know I can call run on it:

Runnable r = ...;
r.run(); // I can guarantee this will work, because whatever "..." evaluates to, implements Runnable

So when you implement Runnable, you are really just saying that "this class has a run method". The Java compiler also checks that what you are saying is true, that the class actually has a run method. Saying false things about your class, intuitively, should not be allowed, right?

We can also think about what would happen if you were allowed to implement Runnable, but not provide a run method, then you would not be 100% sure that any variable of type Runnable has a run method.

class TrueRunnable implements Runnable {
    @Override public void run() {}
}
class FakeRunnable implements Runnable { // suppose this is allowed
    public void myRun() {}
}

...

Runnable r;
if (new Random().nextBoolean()) {
    r = new TrueRunnable()
} else {
    r = new FakeRunnable(); 
}
// does r have a run method? Maybe.

Then interfaces would not be very useful, because you are not 100% sure whether any Object has any method anyway.

Object r;
if (new Random().nextBoolean()) {
    r = new TrueRunnable()
} else {
    r = new FakeRunnable(); 
}
// does r have a run method? Maybe.

So in that case we would lose the whole point of using interfaces.

Upvotes: 1

rzwitserloot
rzwitserloot

Reputation: 103637

Runnable has only one method, thus, you may be tempted to think the method name is irrelevant. But most interfaces have more than one method. For example, java.util.Iterator which has 2 (well, 4, but 2 of those have default impls so you don't need to implement those. Point is, 2 that you must implement).

So, let's say that instead of naming your two methods next and hasNext, you name them jill and jane instead.

How is the compiler or any caller supposed to figure out which one performs the hasNext job and which one performs the next job? Look at the signatures? What if your interface has 2 different methods with the exact same signature?

Not possible. That's why the names need to match: They are used to look this up.

Yes, if the interface has just the one name you'd think the compiler/caller can just go: Yeah, well, the one and only method it has, that's the one. But that's not how it works - language design is simpler if you avoid making exceptions just for simplified cases.

Note that java has lambda syntax, which only works when an interface has one method, and which omit the method name entirely, as well as the argument types. Instead of your myRun method you could write this too:

Runnable r = () -> /* some code */;

No new Runnable() { public void myRun, and no }} required. If myRun had arguments, you don't even need to add their types, just their names. For example, Comparator, which has arguments:

long form:

Comparator<String> lengthComparator = new Comparator<String>() {
    @Override public int compare(String a, String b) {
        return a.length() - b.length();
    }
};

can be written much shorter:

Comparator<String> lengthComparator = (a, b) -> a.length() - b.length();

Upvotes: 2

Related Questions