if i have this interface
public interface someInterface {
public String getValue(String arg1);
public String getValue(String arg1, String arg2);
}
I want to be able to pass 1 or 2 lines to the getValue method without having to override both in each implementation class.
public class SomeClass1 impelments someInterface
{
@Override
public String getValue(String arg1);
}
public class SomeClass2 implements someInterface
{
@Override
public String getValue(String arg1, String arg2);
}
this will not work, because SomeClass1 must implement method 2, and SomeClass2 must implement method 1.
How did i do this?
public interface someInterface2 {
public String getValue(String... args);
}
public class SomeClass3 implements someInterface2
{
@Override
public String getValue(String... args) {
if (args.length != 1) {
throw IllegalArgumentException();
}
}
}
public class SomeClass4 implements someInterface2
{
@Override
public String getValue(String... args) {
if (args.length != 2) {
throw IllegalArgumentException();
}
}
}
someInterface2 someClass3 = new SomeClass3();
someInterface2 someClass4 = new SomeClass4();
String test1 = someClass3.getValue("String 1");
String test2 = someClass4.getValue("String 1, "String 2");
Is there a better way to do this?
source
share