When we create interface methods, can't we do something like in java:
void interface_method(Integer,String, /* other parameter */);
Instead I noticed that we also need to give the parameter names lile:
void interface_method(Integer i, String s);
Also the interface implementor don't need to have the same parameter name as in interface method.
I found a similar question about c# over here . They mention one scenario of named parameter but I don't find any other relevent reason specifically in java.
From a technical standpoint it wouldn't be necessary.
I've always taken it as a syntax normalization, and a documentation aid.
This way there's:
- No reason to differentiate between class and interface method syntax, and
- Default Javadoc documentation can be generated using the (descriptive!) parameter name.
Without parameter names it becomes very hard to distinguish between parameters of the same type.
double divide(double, double);
Convention says that the second parameter would be the divisor. But with named parameters, it is explicit and much clearer. In turn, the documentation can clearly use the names instead having to continually say "first parameter" or "second parameter" when trying to explain how the method should be use and what each parameter is meant to represent.
My take on this:
This is in language specification for parameters to have names;
Interface being interface, names help to communicate the meaning;
Interface is not really a forward declaration of implementation that will follow like in C/C++, so comparison is not entirely correct;
来源:https://stackoverflow.com/questions/10998151/why-do-we-need-to-specify-parameter-name-in-interface