i am preparing for java SE certification exam and playing a little bit with the code. From the theory :
- private methods are not inherited (and therefore cannot be overriden)
- non static class methods cannot be executed without instance of the class
in this sample code I have Parent and Child classes, Parent has a private method test(), Child extends Parent, but since the only non static method in Parent class is private, Child doesn't inherit it. In the Parent's main method I execute test() method on reference (type Parent), which reffers to Child instance. How is it possible that Parent's test() method is executed without instance of Parent?
Winston Gutkowski wrote:
But you do have an instance of Parent.
@Victor: Just to clarify a bit: What Winston says here is correct, but make sure you understand that you have ONE object. That object IS A Child, IS A Parent, IS AN Object, and IS A whatever other classes it extends or interfaces it implements.