Java child class also 'inherits' parent class private fields
Code example to show that Java child class also inherits
parent class private fields.
package space.yuanjiang.java;
public class PrivateFieldInInheritance {
public static void main(String[] args) {
Child c = new Child(5, 10);
c.printIJ();
System.out.println("####");
Parent p = new Child(5, 10);
p.printIJ();
}
}
class Parent {
private int i;
public int j;
Parent(int i, int j) {
this.i = i;
this.j = j;
}
void printIJ() {
System.out.println("i=" + i);
System.out.println("j=" + j);
}
}
class Child extends Parent {
Child(int i, int j) {
super(i, j);
this.j = 15;
}
}
/*
i=5
j=15
####
i=5
j=15
*/
See Private members in Java inheritance for reference.