Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding in Java

Is there a way to override a Java method that the only thing that changes is an ArrayList?

For example:

public void quickSortMethod(ArrayList<Integer> comparison, int start, int end)   
{}   

public void quickSortMethod(ArrayList<Double> comparison, int start, int end)   
{}   

This currently gives me the following error:

Method quickSortMethod(ArrayList<Integer>, int, int) has the same erasure quickSortMethod(ArrayList<E>, int, int) as another method in type QuickSort   

Btw I'm using other objects I created as parameters in the ArrayLists

ArrayList<MyObject>...    
like image 687
Eric Bautista Avatar asked Dec 02 '25 08:12

Eric Bautista


2 Answers

In this case you could try making the method itself generic and using that type as the ArrayList type...

public <T> void quickSortMethod(ArrayList<T> comparison, int start, int end)
like image 144
Robert Rouhani Avatar answered Dec 03 '25 23:12

Robert Rouhani


Unfortunately, no. Because of type erasure after compilation there's no difference between the two.

like image 42
Brian Roach Avatar answered Dec 03 '25 23:12

Brian Roach



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!