I'd like to ask you if it's possible in Java to have a declaration of a method in a interface but, I want that the methods defined could have a variable number of input parameters (for example, all of the same type). I was thinking in something like this:
public interface EqualsCriteria {
public boolean isEqual(String... paramsToCheck);
// this is not equals(Object obj) !!!
}
And one class implements that equal criteria like, for example:
public class CommonEquals implements EqualsCriteria {
private String name;
private String surname;
....
@Override
public boolean isEqual(String otherName, String otherSurname) {
return name.equals(otherName) && surname.equals(otherSurname);
}
}
But maybe, I want other criteria in another part of the code, something like this
public class SpecialEquals implements EqualsCriteria {
....
@Override
public boolean isEqual(String otherName, String otherSurname, String passport) {
return name.equals(otherName) && surname.equals(otherSurname) && passport.equals(passport);
}
}
PS: Actually my problem is a little bit more complicated, but this could be useful to me.
Yes, you can have overloaded methods (methods with the same name different parameters) in an interface. You can implement this interface and achieve method overloading through its methods.
Yes, we can write a method using variable arguments once you use variable arguments as a parameter method while calling you can pass as many numbers of arguments to this method (variable number of arguments) or, you can simply call this method without passing any arguments.
an interface method can't be protected or final. up until Java 9, interface methods could not be private; however, Java 9 introduced the possibility to define private methods in interfaces. interface variables are public, static, and final by definition; we're not allowed to change their visibility.
The interface body can contain abstract methods, default methods, and static methods. An abstract method within an interface is followed by a semicolon, but no braces (an abstract method does not contain an implementation).
You can achieve this by checking size of the array
public class CommonEquals implements EqualsCriteria {
private String name;
private String surname;
....
@Override
public boolean isEqual(String .. arr) {
if (arr.length != 2) {
throw new IllegalArgumentException(); // or return false
}
return name.equals(arr[0]) && surname.equals(arr[1]);
}
}
EDIT
The only way you can do this to have it more readable (extract common part)
@Override
public boolean isEqual(String oName, String oSurname, String .. arr) {
return name.equals(oName) && surname.equals(oSurname); //ignore arr since you don't need it
}
And for the other class you'll have
@Override
public boolean isEqual(String otherName, String otherSurname, String .. arr) {
return name.equals(otherName) && surname.equals(otherSurname) && passport.equals(arr[0]);
}
A better way would be:
public interface EqualsCriteria {
public boolean isEqual(EqualsCriteria other);
public String[] getParam();
// this is not equals(Object obj) !!!
}
And then:
public class CommonEquals implements EqualsCriteria {
private String name;
private String surname;
@Override
public boolean isEqual(EqualsCriteria other) {
if(other==null) return false;
return Arrays.asList(getParam()).equals(Arrays.asList(other.getParam()));
}
}
This way, even if number of Strings changes, still it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With