In this case, you can simply use polymorphism. To do this, you overload your methods - create methods with the same name, but with different types of parameters. java does not check method names, it checks method signatures (method name + parameter + return type), for example:
public class foo { public int add(int a, int b) { int sum = a+b ; return sum ; } public String add(String a, String b) { String sum = a+b ; return sum ; } public static void main(String args[]) { foo f = new foo() ; System.out.printf("%s\n",f.add("alpha","bet)); System.out.printf("%d", f.add(1,2); } }
this code should return
alphabet 3
since you can see that the two method signatures are different, so there is no error. Please note that this is ONLY AN EXAMPLE of what CAN be done.
source share