I have two classes A and B as follows:
public class A {
private int salary = 0;
protected void calculate() {
salary = 400;
}
protected A() {
calculate();
}
}
public class B extends A {
private int salary = 0; // (1)
protected void calculate() {
System.out.println("calculating salary...");
salary = 700;
}
public static void main(String[] args) {
System.out.println(new B().salary); // (2)
}
}
Consider line (2): I don’t understand why new B().salary is valid because the specifier of salary in line (1) is private. Could you help me explain it?
I don’t see any private constructor of
Bsonew B()is valid + the private field is accessible within same class