Constructor overloading and generics

Hi!

I am writing a class with multiple constructors in the following way:

MyClass(ArrayList<X> x) {
...
}

MyClass(ArrayList<Y> y) {
...
}

- however this doesn't compile, since the constructors overlap/conflict.

How can this be modified?

Regards,
Jesper
jespersahnerAsked:
Who is Participating?
 
sciuriwareCommented:
The only solution is to extend ArrayList to your own needs.

;JOOP!
0
 
jespersahnerAuthor Commented:
Hmmm. Guess that is a little simplified.

Using generic methods the following works/compiles:

public <T extends String> MyClass(T x) {
...
}
public <T extends Integer> MyClass(T y) {
...
}

However the following does NOT work/compile:

public <T extends ArrayList<String>> MyClass(T x) {
...
}
public <T extends ArrayList<Integer>> MyClass(T y) {
...
}
0
 
hoomanvCommented:
since ArrayList<String> and ArrayList<Integer> both are indeed an ArrayList
you can not separate them this way
0
 
jespersahnerAuthor Commented:
->hoomanv: Exactly!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.