If i change the byte
to int
I get a compiler error. Could you explain the problem?
public class A {
protected int xy(int x) { return 0; }
}
class B extends A {
protected long xy(int x) { return 0; } //this gives compilor error
//protected long xy(byte x) { return 0; } // this works fine
}
If i change the
byte
toint
I get a compiler error.
If you do that, you have this:
public class A {
protected int xy(int x) { return 0; }
}
class B extends A {
protected long xy(int x) { return 0; }
}
...and the only difference in the xy
methods is their return type. Methods cannot be differentiated solely by their return types, that's the way Java is defined. Consider this:
myInstance.xy(1);
Which xy
should that call? long xy(int x)
or int xy(int x)
?
If your goal is to override xy
in B
, then you need to make its return type int
in order to match A#xy
.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments