Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods for an interface with a variable number of parameters

Tags:

java

interface

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.

like image 201
Manuelarte Avatar asked Aug 07 '13 15:08

Manuelarte


People also ask

Can methods in interface have parameters?

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.

Can a method take variable number of parameters justify?

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.

What is the type of methods and variables in interface?

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.

What types of methods can an interface have?

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).


2 Answers

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]);
}
like image 194
Tala Avatar answered Sep 30 '22 13:09

Tala


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.

like image 41
Jatin Avatar answered Sep 30 '22 14:09

Jatin