• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

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
0
jespersahner
Asked:
jespersahner
3 Solutions
 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now