I have been working on this code and I am not able to figure out why certain access modifiers work and some do not:
public class Base {
protected int method(int x) { return 0; }
}
class Child extends Base {
// Line 1: Compiles
public int method(int x) { return 0; }
// Line 2: Cannot reduce the visibility of the inherited method from Base
private int method(int x) { return 0; }
// Line 3: Compiles
private int method(long x) { return 0; }
// Line 4: The return type is incompatible with Base.method(int)
protected long method(int x) { return 0; }
// Line 5: Compiles
protected int method(long x) { return 0; }
// Line 6: Compiles
protected long method(long x) { return 0; }
// Line 7: Compiles
protected long method(int x, int y) { return 0; }
}
I am trying to understand why lines 1, 3, 5, 6, and 7 are allowed but lines 2 & 4 are not.
I've tried searching but haven't found a clear explanation, though I did find In Java, difference between default, public, protected, and private