Reputation: 193
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;
}
}
Upvotes: 0
Views: 364
Reputation: 427
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
Upvotes: 0
Reputation: 103903
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.
Upvotes: 1
Reputation: 7808
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;
}
}
Upvotes: 0
Reputation: 488
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.
Upvotes: 0