Saving typical types when implemented in a class

I searched many times for general type questions and just did not find anything that helped me understand what I was 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 to create a class that implements this interface. This particular class will use insertion sort, and I need to keep the ArrayList 'E' generic type, so I tried all kinds of things and got 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 a parameter

 ArrayList<E> list 

the compiler clicks on me about the implementation of the supertype method.

I would really appreciate any direction of help. Thanks!

** I cannot mark this as an answer, but it is. I think my problem was that when I had

 SortAnalysis<E> 

I did not have the typical typing specified after the class name. **

+6
source share
2 answers

Specify type type SortAnalysis<E> in

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

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

+3
source

When you declare your InsertionSort class, you implement the raw form of the SortAnalysis class. This means that analyzeSort incorrectly overrides SortAnalysis when you include <E> in the list parameter.

Enter the 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.

+3
source

Source: https://habr.com/ru/post/983011/


All Articles