class One {
void foo() { }
}
class Two extends One {
private void foo() { /* more code here */ }
}
Why is the above snippet of code wrong?<
Because inheritance is a is a relationship. Anyone could refer to a instance of Two through a reference to One :
One v = new Two();
What would the program do if you called the foo method on the v reference? You broke the public contract of One, which guarantees that every instance of One has a (here package-protected) foo method. That's why the compiler forbids it.