• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading and Overriding

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Can you please tell me what would be the outcome of the below program and the reason behind such outcome?

Thanks & Regards,
Sukesh.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sukesh, welcome to javaranch

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...
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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().
 
Sukesh Jain
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Tom,

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.

Thanks & Regards,
Sukesh.

PS @Ankit: Thanks for the code tag information.
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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().
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic