On the the following code sample:
class Test6 {
void met1(long x, long y) { System.out.println("long"); }
void met2(int x, int y) { System.out.println("int"); }
public static void main(String[] args) {
Test6 t = new Test6();
t.met1(10, 6);
}
}
The more specific match (long) is choosen and the answer "long" is printed.
But on the following:
class Rectangle {
public int area(int length, int width) {
return length * width;
}
}
class Square extends Rectangle {
public int area(long length, long width) {
return (int) Math.pow(length, 2);
}
}
class Test4 {
public static void main(String[] args) {
Square r = new Square();
System.out.println(r.area(5, 4));
}
}
generates the following error:
reference to area is ambiguous, both method area(int,int) in Rect
angle and method area(long,long) in Square match
I don't understand why the more specific match rule doesn't apply in this case. The area method should be overloaded and the Square class should contain both of the area methods, so that it reduces to the same situation as in the first example.
Clearly, I don't understand something correctly