My java book has the following practice question:
public class Person(){...}
public class Teacher extends Person{...}
And it asks which of the following are true statements:
1: Teacher inherits the constructors of Person
.
2: Teacher can add new methods and private instance variables.
3: Teacher can override existing private methods of Person
.
The book says 2 and 3 are true. I said only 2 is true.
I have read that subclasses do indeed inherit private methods and member fields yet they cannot be directly accessed. So my question is how would one override existing private methods if they cannot be directly accessed? And why would one want to override private methods if they were probably made private for a good reason?
3: Teacher can override existing private methods of Person.
That's wrong.
Private methods/fields of super class are not visible/inherited to sub classes. So, You can't override them.
Try it with an example