Considering the following code:
class Point{
void aMethod(int f){
int var = f;
System.out.println("This is the superclass"+var);
}
}
public class Subpoint extends Point{
public static void main(String[] args){
Point s=new Subpoint();
s.aMethod(2.1f);
}
void aMethod(float f){
float var = f;
System.out.println("this is subclass"+var);
}
}
when compiled, error" aMethod(int) can not be applied to (float)
s.aMethod(2.1f);" occurs
I am confused about,since s references a Subpoint object,why does't compiler invoke aMethod(float) defined in Subpoint class without error?
I remember that when invoke an instance method, the method defined in the class whose object is being referenced will be invoked.
Am I wrong?
Thanks for all replies.