I'm currently learning JAVA and got confused on the access in inheritance. The case below:
public class Father{
private int age;
public void setAge(int a){
age = a;
}
public void getAge(){
System.out.println(age);
}
} //End of Father
public class Son extends Father{
} //End of Son
public class Test{
public static void main(String[] args) {
Father F = new Father();
Son S = new Son();
F.setAge(40);
F.getAge();
S.setAge(20);
S.getAge();
//System.out.println(F.age);
}
} //End of Test
In the case above, "age" is a private variable in Father. Although Son extends Father, this private variable "age" is not inherited. Which means there is no variable in Son.
But when I put them into Test, running result shows "40" and "20", as if there was an int variable in Son. How to explain this?