Note that there are some explanatory texts on larger screens.

plurals
  1. POInvoking a method overloaded where all arguments implement the same interface
    text
    copied!<p>My starting point is the following:<br> - I have a method, transform, which I overloaded to behave differently depending on the type of arguments that are passed in (see transform(A a1, A a2) and transform(A a1, B b) in my example below)<br> - All these arguments implement the same interface, X</p> <p>I would like to apply that transform method on various objects all implementing the X interface.</p> <p>What I came up with was to implement transform(X x1, X x2), which checks for the instance of each object before applying the relevant variant of my transform.</p> <p>Though it works, the code seems ugly and I am also concerned of the performance overhead for evaluating these various instanceof and casting. Is that transform the best I can do in Java or is there a more elegant and/or efficient way of achieving the same behavior?</p> <p>Below is a trivial, working example printing out BA. I am looking for examples on how to improve that code. In my real code, I have naturally more implementations of 'transform' and none are trivial like below.</p> <pre><code>public class A implements X { } public class B implements X { } interface X { } public A transform(A a1, A a2) { System.out.print("A"); return a2; } public A transform(A a1, B b) { System.out.print("B"); return a1; } // Isn't there something better than the code below??? public X transform(X x1, X x2) { if ((x1 instanceof A) &amp;&amp; (x2 instanceof A)) { return transform((A) x1, (A) x2); } else if ((x1 instanceof A) &amp;&amp; (x2 instanceof B)) { return transform((A) x1, (B) x2); } else { throw new RuntimeException("Transform not implemented for " + x1.getClass() + "," + x2.getClass()); } } @Test public void trivial() { X x1 = new A(); X x2 = new B(); X result = transform(x1, x2); transform(x1, result); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload