Java – how do I overload methods in an interface?
If I had this interface
public interface someInterface { // method 1 public String getValue(String arg1); // method 2 public String getValue(String arg1,String arg2); }
I want to be able to pass 1 or 2 strings to the getValue method without overwriting them 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 does not work because someclass1 needs to implement method 2 and someclass2 needs to implement method 1
Do I insist?
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(); } // code } } public class SomeClass4 implements someInterface2 { @Override public String getValue(String... args) { if (args.length != 2) { throw IllegalArgumentException(); } // code } } 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?
Solution
Interface is used as a contract for the user of the interface: specify the available methods (in all implementations) and how to call them If two implementations of an interface require different methods, the method should not be part of the interface:
public interface Lookup { } public class MapLookup implements Lookup { public String getValue(String key) { //... } } public class GuavaLookup implements Lookup { public String getValue(String row,String column) { // ... } }
In your program, you will know the implementation you are using, so you only need to call the correct function:
public class Program { private Lookup lookup = new MapLookup(); public void printLookup(String key) { // I hardcoded lookup to be of type MapLookup,so I can cast: System.out.println(((MapLookup)lookup).getValue(key)); } }
Alternative methods
If your class program is more generic and uses dependency injection, you may not know which implementation you have Then, I will create a new interface key, which can be any key:
public interface Lookup { // ... public String getValue(Key key); } public interface Key { } public MapKey implements Key { private String key; // ... } public GuavaKey implements Key { private String row,column; // ... }
Dependency injection in programs may come from some factory implementations Since you cannot know which type of lookup to use, you need a getValue contract
public interface Factory { public Lookup getLookup(); public Key getKey(); } public class Program { private Lookup lookup; public Program(Factory factory) { lookup = factory.getLookup(); } public void printLookup(Factory factory) { System.out.println((lookup.getValue(factory.getKey())); } }