This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
As your code stands method1() is not inherited to Child class so it's not an override in the Child class. So Parent class methods called. But if you make the modifier default/public/protected of method1() in Parent then the method inherited so overriden by the Child class. Hence method1() of Child class called.
Because you are not overriding method1. Overriding and polymorphism are not applicable for private methods. So instead of overriding method1, you are creating a new method that just happens to have the same signature as the private method.
If you would make method1 protected in Parent you would get what you expect.
In that case there is no access to a private method from the subclass. If you have a method of the same name, the compiler has to ignore the previous method.
Remember a private method is entirely for "internal consumption;" it is only for use inside its own class. When you create a new method with the same name, the compiler sees that as something completely new and independent.
Sagar Rohankar: Now this method2 code is inserted by compiler with call to method1() in it
I'm not sure about that the compiler inserts that part in to child code or at the runtime it goes for the Parent object and execute method2() when it sees that child doesn't override that method. In that case Parent's method1() will be executed since it can't access the private method of Child from there (This is what Campbell Ritchie means i think). Correct me if I'm wrong.
Joined: Sep 17, 2008
In that case Parent's method1() will be executed since it can't access the private method of Child from there
so, your saying look up is comes from parent to child. first go to the parent and look if the method can override the child's method. if it can override, use child's method if it can't, use parent's method. mmm... according to my knowledge it's other way around. (correct me if i'm wrong)
first, look on child's class and if not found go to the parent.