Is there a way to return a different type in a subclass method?
I have 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
⌛
This post has been reserved for your question.
Hey @blockgoblin31! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
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
oh it is?
(damn it)
thats my 3rd try to come up with a name
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)
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
Are you sure that List.of() wouldn't do it?
Im not familliar with that method, so probably
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•2y ago
Message Not Public
Sign In & Join Server To View
(yes, but it's annoying)
(Also List.of() is a bit shorter than Arrays.asList())
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
well now I feel like im complaining about nothing because the class also has(as a new method)
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
(Actually, there is nothing convenient for a mutable ArrayList created with a non-collection set of initial values)
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Some variables that come from elsewhere
In some cases you may even need a bunch of literal values
I mostly made it a seperate class for Adding that was just because it would be nice
Post Closed
This post has been closed by <@501514065068294154>.