Skip to content
Advertisement

Tag: overloading

Varargs in method overloading in Java

The following code doesn’t compile. A compile-time error is issued. reference to test is ambiguous, both method test(int…) in varargspkg.Main and method test(float…) in varargspkg.Main match It seems to be obvious because the parameter values in the method call test(1, 2); can be promoted to int as well as float If anyone or both of the parameters are suffixed by

Java erasure with generic overloading (not overriding)

I have FinanceRequests and CommisionTransactions in my domain. If I have a list of FinanceRequests each FinanceRequest could contain multiple CommisionTransactions that need to be clawed back. Dont worry how exactly that is done. The class below (very bottom) makes me feel all fuzzy and warm since its succint and reuses existing code nicely. One problem Type erasure. They both

How to do method overloading for null argument?

I have added three methods with parameters: When I am calling doSomething(null) , then compiler throws error as ambiguous methods. So is the issue because Integer and char[] methods or Integer and Object methods? Answer Java will always try to use the most specific applicable version of a method that’s available (see JLS ยง15.12.2). Object, char[] and Integer can all

Overload with different return type in Java?

Why is it not possible to overload a function just by changing the return type? Will that change in a future version of Java? By the way, just for reference, is this possible in C++? Answer You can’t do it in Java, and you can’t do it in C++. The rationale is that the return value alone is not sufficient

Advertisement