0

I have an interface SortingToolTemplate which contains an ArrayList as an instance variable.

Since I'll have many classes implementing this interface but with different data types for the ArrayList I declared it like this:

ArrayList<Object> data = null;

This way each class can instanciate it like :

data = new ArrayList<Integer>(); OR data = new ArrayList<String>(); 

etc...

When I do that I get an error saying "unexpected token" in my IDE, but if I remove that line I can use the ArrayList without even instantiating it.

Anyone knows why this is happening ?

EDIT

The interface code :

interface SortingToolTemplate {

ArrayList<Object> data = null;

int getMax();
int getMaxOccurrence(int max);
void getData();
void showResult();
int getMaxPercentage();

}

One of the classes code(not implemented yet, but already showing the error):

public class WordSort implements SortingToolTemplate {

data = new ArrayList<String>();

@Override
public int getMax() {
    return 0;
}

@Override
public int getMaxOccurrence(int max) {
    return 0;
}

@Override
public void getData() {

}

@Override
public void showResult() {

}

@Override
public int getMaxPercentage() {
    return 0;
}

}

momodjib
  • 183
  • 8
  • Show us the code so we can reproduce your issue. – nicomp Feb 10 '20 at 16:28
  • 1
    You'll need to make use of Java's generics, but can't guide you to a solution without seeing the rest of the minimally reproducible code. see https://docs.oracle.com/javase/tutorial/java/generics/types.html for generics – JamieT Feb 10 '20 at 16:30
  • On why you cannot say `List list = new ArrayList()` in java: [Is List a subclass of List? Why are Java generics not implicitly polymorphic?](https://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-are-java-generics-not-implicitly-po) – OH GOD SPIDERS Feb 10 '20 at 16:39

4 Answers4

1

There are 2 separate problems going on here.

[1] You cannot put random statements in classes or interfaces; inside a class or interface you can only put fields, methods, constructors, (static) initializers, and other type definitions. You're adding a statement (list = new ArrayList<String>();) - you can't do that. Move that to a constructor, or just initialize it straight up: List<String> list = new ArrayList<String>();)

[2] you cannot assign a list of strings to a list of objects, because they are not the same. You can add an integer to a list of objects (because integer is an object), but, now there's an integer in list of strings, which is why java won't let you. The fix is to have a List<?> list = new ArrayList<String>(); which is fine, but note that you can't add anything to a List<?>. (except null).

What you probably want is something like:

public class WordSort<E> implements SortingToolTemplate {
    private List<E> list = new ArrayList<E>();
}

note also that you cannot declare fields in interfaces at all. It might look like you can, but you're declaring a constant (they are public, static, and final, even if you don't say so). That's not what interfaces are for.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thanks for the detailed answer, still learning all this. I'll go with an abstract class instead of an interface and just instantiate the ArrayList inside every class with the correct data type. – momodjib Feb 10 '20 at 17:25
0

I don't know what's going on with your code, but I'd use generics instead of Object.

I'd also use List in the left side, so someone can use a different implementation, for example, LinkedList.

Eduardo Pérez
  • 488
  • 3
  • 10
0

You need to parametrize your interface. Here is a simplified example:

public interface MyInterface<T> {
...
  List<T> getList();
}

public class MyFirstImplementation implements MyInterface<Integer> {
  private List<Integer> myList = new ArrayList<>();

  @Override 
  public List<Integer> getList() {
    return myList;
  }
}

public class MySecondImplementation implements MyInterface<String> {
  private List<String> myList = new ArrayList<>();

  @Override 
  public List<String> getList() {
    return myList;
  }
}
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

A String is an Object, but an Object does need to be a String. This is why: List list = new ArrayList() or new ArrayList() does not work.

If you look to ArrayList class, you will see that it uses Generics. Some guys suggested and created some code to you. You should follow.

I have something that may help you: Sort List

sgtcortez
  • 407
  • 4
  • 15