Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two identically-named functions with different parameters, one without a scope resolution operator, the other with

Tags:

java

I'm in the process of converting a program from C++ to Java. In my C++ code, I have two functions within class SomeClass that have the same name, but with different parameters, and with one function using a scope resolution operator and the other without.

SomeOtherType* SomeClass::foo()
{
//some code
}

and

SomeOtherType* foo(list<Token*>& param)
{
//some more code
}

Since Java doesn't use scope resolution operators, how do I implement these functions equivalently in Java?

Thanks in advance.

like image 783
Johnny Avatar asked Feb 11 '23 10:02

Johnny


1 Answers

Overloading basically works the same in Java. You can define multiple methods with the same name as long as their parameters differ. So you can just write:

public SomeOtherType foo() {
  return null;
}

public SomeOtherType foo(List<Token> tokens) {
  return null;
}
like image 124
Robin Krahl Avatar answered Feb 13 '23 04:02

Robin Krahl