Sukesh please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
The YourKnowledgeTest.test() is private so YourExtensiveKnowledgeTest.test() is not overriding it. It is simply creating a method with the same name. Since your main() method is part of YourKnowledgeTest, you have access to the private method. Because you are assigning a reference to the YourExtensiveKnowledgeTest instance in a YourKnowledgeTest variable, the compiler only sees YourExtensiveKnowledgeTest as a YourKnowledgeTest and therefore calls the only test() method it knows about (the private one).
Note that had you moved the main() method to YourKnowledgeTest then you would have gotten a compiler error because the compiler does not have access to the private method test().
Joined: Jun 27, 2010
Thanks for the explanation.
I thought the invocation of method is determined at runtime and not at compilation time. In this case, either it should result in runtime error as there is no overriding test method.
Sukesh Jain wrote:I thought the invocation of method is determined at runtime and not at compilation time. In this case, either it should result in runtime error as there is no overriding test method.
There's no error here. YourKnowledgeTest.test() is private, so YourExtensiveKnowledgeTest.test() does not override it. This means that when you call test.test() in your code, it calls YourKnowledgeTest.test().