public class WrapperClasses{
void overloadedMethod(Number N){
System.out.println("Number Class Type");
}
void overloadedMethod(Double D){
System.out.println("Double Wrapper Class Type");
}
void overloadedMethod(Long L){
System.out.println("Long Wrapper Class Type");
}
public static void main(String[] args){
int i = 21;
WrapperClasses wr = new WrapperClasses();
//wr.overloadedMethod(i);
}
}
class mine extends WrapperClasses{
void overloadedMethod(int N){
System.out.println("Integer Class Type");
}
public static void main(String[] args){
int i = 21;
WrapperClasses wr = new mine();
wr.overloadedMethod(i);
}
}
This prints Number Class Type
.
I understand the rules of wrapper class method overloading:
According to rule 1, it should print Integer Class Type
. What am I missing here?
At a language spec level, it is because methods with parameters which differ as being primitive and wrapped primitive types are not considered as override-equivalent. (A fancy way of saying "they just don't because the language spec says so").
But logically, they shouldn't either, at least in the case of an int
parameter in a subclass "overriding" a wrapped parameter in a superclass.
By Liskov's Substitution Principle, methods in subclasses must accept at least all the parameters accepted by the method in the superclass.
If the superclass method accepts the wrapped class, it can accept null
. If the subclass method were allowed only to accept int
, it could not accept null, so it would not be substitutable.