Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need to specify parameter name in interface?

Tags:

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.

like image 343
Priyank Doshi Avatar asked Jun 12 '12 13:06

Priyank Doshi


People also ask

What is the purpose of the interface parameter?

This name is used in the statements in the procedure to refer to the value that will be passed as an actual parameter when the procedure is called. The mode of the formal parameter is in, indicating that it is used to pass information into the procedure from the caller.

Do parameter names and argument names have to be the same?

The caller's arguments passed to the function's parameters do not have to have the same names.

Can an interface be used as a parameter?

Yes, you can pass Interface as a parameter in the function.

Which method is used to get all the parameter names?

You can obtain the names of the formal parameters of any method or constructor with the method java. lang.


1 Answers

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:

  1. No reason to differentiate between class and interface method syntax, and
  2. Default Javadoc documentation can be generated using the (descriptive!) parameter name.
like image 138
Dave Newton Avatar answered Sep 28 '22 07:09

Dave Newton