Reputation: 413
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>...
Upvotes: 2
Views: 1298
Reputation: 943
It is a valid example of overriding and here is the also answer of Can we override the static method? Answer is NO.
here is the sample code.
package com.sample.test;
class Animal {
public static void showMessage() {
System.out.println("we are in Animal class");
}
}
class Dog extends Animal {
public void DogShow() {
System.out.println("we are in Dog show class");
}
public static void showMessage() {
System.out.println("we are in static overridden method of dog class");
}
}
class Cat extends Animal{
public static void showMessage(){
System.out.println("we are in overridden cat class");
}
}
public class AnimalTest {
public static void main(String[] args) {
Animal animal = new Animal();
animal.showMessage();
Dog dog = new Dog();
dog.showMessage();
Animal animal2 = new Dog();
animal2.showMessage();
Animal animal3 = new Cat();
animal3.showMessage();
}
}
OUTPUT:
we are in Animal class we are in static overridden method of dog class we are in Animal class we are in Animal class
Upvotes: 0
Reputation: 4502
Since you will be using those array lists as parameter objects, you could define your own custom objects which inherit from Arraylist. This way, the compiler will recognize your overload as having distinct signatures.
Of course, this means any client code WILL need to know which sub-type to pass. But that should be a relatively easy problem to address, becaus eif you know you are feeding your parameter object ints, then initialize an integerParamList. Likewise for doubles.
Without knowing a little more about the rest of your code, this may or may not be an appropriate solution, but it may get close? (I wasn't able to test this, so there may be unknown issues . . .)
public class mySortingClass {
public void quickSortMethod(IntegerParamList comparison, int start, int end)
{}
public void quickSortMethod(DoubleParameterList comparison, int start, int end)
{}
}
public class IntegerParamList extends ArrayList<Integer>
{}
public class DoubleParameterList extends ArrayList<Integer>
{}
Upvotes: 0
Reputation: 82337
You could make that into one function with
ArrayList<Object>,int,int
and then based on the object type of arraylist employ different analysis
Upvotes: 0
Reputation: 14688
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)
Upvotes: 10
Reputation: 76918
Unfortunately, no. Because of type erasure after compilation there's no difference between the two.
Upvotes: 3
Reputation: 309008
The error tells you the answer: You can't do what you're proposing.
http://docs.oracle.com/javase/tutorial/java/generics/erasure.html
Upvotes: 0