Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping generic types when implementing in class

I've done a lot of searching through generic type questions and just haven't found anything that has helped me figure out what I am doing wrong here. I have an interface as follows:

public interface SortAnalysis<E extends Comparable<? super E>> 
{
    public long analyzeSort(ArrayList<E> list);
}

Now, the next step is making a class that implements this interface. This particular class is going to use an insertion sort and I need to keep the ArrayList type 'E' generic, so I tried all sorts of things and ended up with the following:

public class InsertionSort<E extends Comparable<? super E>> implements SortAnalysis {

@Override
public long analyzeSort(ArrayList list) {
    // TODO Auto-generated method stub
    return 0;
}

My problem is that when I try to do this for the parameter

ArrayList<E> list

the compiler gripes at me about implementing a supertype method.

I would really appreciate any direction of help. Thanks!

**I can't mark this as answered yet, but it is. I think my problem had been that when I had

SortAnalysis<E>

I did not have the generic typing listed after the class name.**

like image 892
sebe Avatar asked Feb 26 '15 00:02

sebe


2 Answers

Specify the type like SortAnalysis<E> in

public class InsertionSort<E extends Comparable<? super E>> 
        implements SortAnalysis<E> {

when you omit it you have a raw-type and not a generic version.

like image 121
Elliott Frisch Avatar answered Oct 26 '22 07:10

Elliott Frisch


When declaring your InsertionSort class, you are implementing the raw form of the SortAnalysis class. That means that analyzeSort isn't properly overriding SortAnalysis when you include <E> on the list parameter.

Supply a type parameter in the implements clause.

class InsertionSort<E extends Comparable<? super E>>
    implements SortAnalysis<E> {
//                         ^^^

Now you can include <E> in the list parameter.

like image 32
rgettman Avatar answered Oct 26 '22 08:10

rgettman