I have some class public class myClass implements A, B where A and B both contain a method public int doSomething();, but A.doSomething is specified by the interface to do something different than B.doSomething.
I have read Two interfaces with same method signature implemented in Java class, but that doesn’t really address my problem, because the methods are overriden to do the same thing, but as I said above, my question is about when they are specified in the interfaces to do different things.
For example, suppose A.doSomething() is supposed to return 0, while B.doSomething() is supposed to throw an exception, and violating either one wold cause problems for methods that are supposed to take them as parameters.
Is there any way to do this in java? If so, how would one actually do it?
You might be able to use a
Proxyinstance to do this. See this question for info onProxy(particularly, the second part of the answer.)The
InvocationHandlerthat you write would check to see which interface is being used to call the method and delegate to the appropriate method inside your object. Here’s what your implementation looks like:Then your InvocationHandler:
Then to create the proxy, you’ll pass in both interfaces:
I think this will work. When you call it using one interface or the other:
Then it should pass in
Methodobjects with different declaring classes. I’m not sure if this is how it works though, so this will need to be tested.