#How do you change the return type of a child-class method inherited from the parent

1 messages · Page 1 of 1 (latest)

spiral panther
#

parent:

public int[] GetNums() {
  return new int[] {a,b,c};
}

child:

float[] GetNums() {
  return new float[] {a,b,c};
}

This doesn't work, is there any reasonable workaround for this?

bleak vaporBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
Parent: ```java public int[] GetNums() { return new int[] {a,b,c}; } ``` child: ```java float[] GetNums() { return new float[] {a,b,c}; } ``` This doesn't work, is there any reasonable workaround for this?

In Java, you cannot change the return type of an overridden method. The return type must be the same or a subtype of the return type in the parent class. In this case, you can't change int[] to float[].

A workaround would be to keep the parent method's return type as int[] and then manually convert the array to float in the child class method.

Useful links:

native veldt
#

You don't

#

You can only have subtypes as the return types in overridden methods

proper pond
pearl sequoia
#

there is Number as common supertype but it likely wont really do what u want either

#

maybe u can explain ur use case a bit and we find a proper solution

#

🙂

rose scaffold
#

As others have said you can't do that. But, you can return Object instead in the parent, if you control that class.

// in parent class
public Object GetNums() {
  return new int[] {a,b,c};
}

// in subclass
public Object getNums(){
  return new float[] {a,b,c};
}

That is the only way to return ANY type (that's not a primitive). All arrays, even primitive ones are technically Object. So this is the only way to work generically with arrays of primitives. You would be required to cast the Object back to it's original int[] or float[] where it is used, which is not ideal.

Now, people will yell at you for doing this. The logical idea being that you would use the wrapper class of primitive types (Integer, Float, Double, Long) instead and return List<T> where T is Integer, Float, etc.

public List<T> getNums(){
  return List.of(a,b,c);
}

You could also return just a T where T would be an int[] or float[] since those are still reference types. This is slightly better than returning Object since you won't need a cast where it's used.

public T getNums(){
  return new int[]{a,b,c};
}
odd prairie
#

Another issue is that you generally want a super type to give a "less specific" promise about what it returns

#

And then a subtype to give a "more specific" one

#

int[] and float[] don't have that relationship. They are just different

#

You could have something like

#
sealed interface Numeric {
    double doubleValue();
}

sealed interface IntegerNumeric extends Numeric {
    int intValue();
}
#

And then have the super type give a Numeric[] and the subtype give a IntegerNumeric[]

#

Or vice versa

#

But you see how this opens up a whole can of worms over just using doubles

rose scaffold
#

I think OP has implemented some algorithm, it's in a class instead of just being a static utility method, and this getNums method is a way to get the result. And now he want's to implement with float, double, etc.

bleak vaporBOT
#

@spiral panther

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍