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:
- If you are passing a primitive data type as an argument to the method call, the compiler first checks for a method definition which takes the same data type as an argument.
- If such a method does not exist, then it checks for a method definition which takes a larger-sized primitive data type than the passed data type. I.e., it tries to perform auto-widening conversion of the passed data type.
- If auto-widening conversion is not possible, then it checks for a method definition which takes the corresponding wrapper class type as an argument. I.e., it tries to perform auto-boxing conversion.
- If such a method does not exist, then it checks for a method which takes the super class type (Number or Object type) as an argument.
- If such a method also does not exist, then the compiler gives a compile-time error.
According to rule 1, it should print Integer Class Type
. What am I missing here?
WrapperClasses wr = System.millis() % 2 == 0? new WrapperClasses() : new mine();
that could become unsolvable. – Sullen