-2

looking for some help... In Java - i am trying to find if i can have a helper class which basically has several methods:

 class helper{
method1(){doing something}
method2(){doing something}
method_important(String a, String b){doing something}
method_important(String a, int b, String b){doing something}
}

Notably i want to call helper class with method_important from other classes but want a flexiblity to pass desired arguments as i it can be two strings or i may have to pass String, int, String.... is it even possible? if no? what is best way to achieve it? Problem is from my other methods i need to pass different type of variables..

Nik
  • 191
  • 2
  • 11
  • I don't think you need a helper class, you can just overload your methods. See this https://stackoverflow.com/questions/21058166/is-it-possible-multiple-methods-with-the-same-name-but-different-parameters-in-a it might help. – Meepo Dec 05 '17 at 06:59
  • As shared in your code, method overloading is the best way to achieve what you want to do. And as it seems you've already done it.. – Dogukan Zengin Dec 05 '17 at 07:00
  • java provides method overloading to achieve this functionality – Lalit Verma Dec 05 '17 at 07:00
  • Possible duplicate of [Java method overloading](https://stackoverflow.com/questions/9598166/java-method-overloading) – Lalit Verma Dec 05 '17 at 07:09

2 Answers2

1

Although, the best way and suitable to you is using Method Overloading (specially in case where arguments have different data types). But in case and just in case , if you have several methods where type of arguments is same but the number of arguments different (which can be a probable case), Why not use Varags https://docs.oracle.com/javase/8/docs/technotes/guides/language/varargs.html

You can use a combination of both Method overloading and varags in you helper class.

voucher_wolves
  • 565
  • 10
  • 20
0

That's perfectly posible in Java, and it is called method overloading. Check this out https://www.javatpoint.com/method-overloading-in-java

Cristian
  • 370
  • 3
  • 8