Is there a way to return a different type in a subclass method?

I have this
public class List<E> extends java.util.ArrayList<E> {
public List<E> add(E input) {
super.add(input);
return this;
}
public class List<E> extends java.util.ArrayList<E> {
public List<E> add(E input) {
super.add(input);
return this;
}
which seems to not be valid because the method .add(E) in ArrayList returns a boolean. Is there any way I can implement this?
18 Replies
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @blockgoblin31! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan2y ago
Of course not, that would be a direct violation of polymorphism. Make a new, independant method Also you really shouldn't name your class List, since it's the name of a Java famous interface, and a superinterface of ArrayList
blockgoblin31
blockgoblin31OP2y ago
oh it is? (damn it) thats my 3rd try to come up with a name
Kyo-chan
Kyo-chan2y ago
What the hell does that new list do, or what the hell is that new list? Name it after that (Note that fundamentally, it's rarely smart to extend existing collections, nor even to make new ones. It happens, but it's rare)
blockgoblin31
blockgoblin31OP2y ago
basically it fixes a couple things I found mildly annoying with ArrayList this is the last one, not being able to use .add() when defining the variable
Kyo-chan
Kyo-chan2y ago
Are you sure that List.of() wouldn't do it?
blockgoblin31
blockgoblin31OP2y ago
Im not familliar with that method, so probably
Kyo-chan
Kyo-chan2y ago
List.of() produces an unmodifiable List, so it's not always the solution For some circumstances, I made my own collections builder too. But it does not extend the collections
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kyo-chan
Kyo-chan2y ago
(yes, but it's annoying) (Also List.of() is a bit shorter than Arrays.asList())
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
blockgoblin31
blockgoblin31OP2y ago
well now I feel like im complaining about nothing because the class also has(as a new method)
public List<E> add(E[] input) {
for (E element : input) {
this.add(element);
}
return this;
}
public List<E> add(E[] input) {
for (E element : input) {
this.add(element);
}
return this;
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kyo-chan
Kyo-chan2y ago
(Actually, there is nothing convenient for a mutable ArrayList created with a non-collection set of initial values)
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Kyo-chan
Kyo-chan2y ago
Some variables that come from elsewhere In some cases you may even need a bunch of literal values
blockgoblin31
blockgoblin31OP2y ago
I mostly made it a seperate class for
public List<E> sortBy(List<E> key) {
List<E> output = new List<>();
for (E o : key) {
int j = this.getCount(o);
for (int i = 0; i < j; i++) {
output.add(o);
}
}
this.set(output);
return output;
}
public void scheduleRemove(E e) {
this.toRemove.add(e);
}
public void applyRemove() {
this.remove(toRemove);
toRemove.clear();
}
public List<E> sortBy(List<E> key) {
List<E> output = new List<>();
for (E o : key) {
int j = this.getCount(o);
for (int i = 0; i < j; i++) {
output.add(o);
}
}
this.set(output);
return output;
}
public void scheduleRemove(E e) {
this.toRemove.add(e);
}
public void applyRemove() {
this.remove(toRemove);
toRemove.clear();
}
Adding that was just because it would be nice
JavaBot
JavaBot2y ago
Post Closed
This post has been closed by <@501514065068294154>.

Did you find this page helpful?