return a specific type when Object is the return type in a method in Java

java, methods, object, return

Solution

You could use generics:

public class MyClass<T extends Number>{


  public T getValue(){
    //do something here
  }
}

MyClass<Integer> foo = new MyClass<Integer>();
foo.getValue()+5;

Problem

I have a method `getValue` like this ``` public Object getValue() { return Integer.valueOf(0); ``` } and a call in the main method: ``` getValue() + 5; ``` This is simplified. How to get this working without casting in the main method, but instead how to cast the return type if possible?

Original source