Search code examples
javaoverridingparameterized-types

Why Java has so many parse methods?


For example the Math.max( .. .. ) method is overloaded to support different types of Number. One name for all of them because, no mather the type of Number, it does the same thing.
So does the parseNumberType methods defined for each class extending Number.

Why is it the way it is ? Wouldn't have been better if a parameterized parse method was defined in Number ( and Number of course, parameterized ) like : public abstract T parse( String s ); ( and implemented specifically afterwards in all the subclasses of Number )


Solution

  • Number exists since JDK 1.0, Generics were only introduced in Java 1.5. Such a generic parsing method would therefore not been possible.

    Concerning Math.max, it accepts only primitive types, which do not have any form of relationship to each other, and every primitive type needs to be declared separately to be supported.