Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Overloading in java - using List Type [duplicate]

class Value {
    public void method1(List<Integer> intList) {

    }

    public void method1(List<Double> doubleList) {

    }

}

Not able to use function overloading in the above two methods.

This looks like both the methods are taking List as a parameter. Is there a way to differentiate the parameters on the data type of Lists?

This is the error message:

Erasure of method method1(List<Integer>) is the same as another method in type Value

Is there any other way through which I can use overloading here?

like image 654
Ankit Deshpande Avatar asked Dec 13 '25 21:12

Ankit Deshpande


1 Answers

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart. See oracle docs.

like image 122
cihan adil seven Avatar answered Dec 15 '25 10:12

cihan adil seven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!