I have this piece of code
public class Base {
private int x=10;
void show(){
System.out.println(x);
}
}
public class Child extends Base {
public static void main(String[] args) {
Child c1=new Child();
c1.show();
}
}
This piece of code is working fine and output is 10.Can anyone please
Elaborate how this private data member is access in child class..
It isn’t. The
show()method is accessed. That method of the parent then accesses the fieldx. Theshow()method has default access, which includes access by theChildas it is in the same package.