class Super {
protected int a;
protected Super(int a) { this.a = a; }
}
class Sub extends Super {
public Sub(int a) { super(a); }
public Sub() { this.a = 5; }
}
public Sub() { this.a = 5; }
this.a=5 doesn’t work. Why is this so? Protected and public members should be inherited.
The problem isn’t that you access the variable, but that you don’t call the base constructor:
This happens because you didn’t define a default constructor for
Super, so derived classes don’t know which constructor to call if you’re not specifying one.