Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does EL support overloaded methods?

Tags:

overloading

el

I upgraded my Java EE web application to use newer PrimeFaces version and suddenly the call of an overloaded bean method in an action attribute of PrimeFaces commandlink did not work anymore. I tried to use JSF default commandlink to test it and this one did not work either.

The method signatures are as follows:

public void updateA(B b); public void updateA(A a); 

It always tried to cast A to B.

More curious, how could it work before the upgrade?

like image 582
djmj Avatar asked Mar 19 '12 00:03

djmj


People also ask

Which methods can be overloaded?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading. For example: void func() { ... }

Can an interface have overloaded methods?

Yes, you can have overloaded methods (methods with the same name different parameters) in an interface. You can implement this interface and achieve method overloading through its methods.

What Cannot overload method?

Can we overload methods that differ only by static keyword? We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example.

Is it allowed to override an overloaded method?

So can you override an overloaded function? Yes, since the overloaded method is a completely different method in the eyes of the compiler.


1 Answers

EL does not support it, no. It'll always be the first method of the Class#getMethods() array whose name (and amount of arguments) matches the EL method call. Whether it returns the same method everytime or not depends on the JVM make/version used. Perhaps you made a Java SE upgrade in the meanwhile as well. The javadoc even says this:

The elements in the array returned are not sorted and are not in any particular order.

You should not rely on unspecified behaviour. Give them a different name.

like image 163
BalusC Avatar answered Oct 05 '22 10:10

BalusC