Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

binding of methods

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I apologize because I know this topic has been brought up before and I should click on all the links that come up on it when I do a search. I have some last minute review stuff I have to go over before I take this exam tomorrow.
I'm curious about the code below:

I understand that if method1 in Parent was changed to public that the child's method1 would end up being called when this was run. I'm just not sure exactly why making the method private in parent messes things up. I understand that private methods aren't inherited but why should that matter if Child has it's own version of method1. I know it has something to do with when things are bound etc., but I'm still confused on this topic. Any light much appreciated.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rick,
when you declare a method private, first it is not inherited and second it cannot be overridden. That means that you cannot "specialize" your private method in a subclass, or in clear, you cannot use polymorphism. If you invoke method1 (which is private) from within the Parent class, then method1 in Parent will be invoked because it cannot be found (read overridden) somewhere else since it is private.
JLS 15.12 Method Invocation Expression explains this in details.
The february newsletter about the same topic: JLS 15.12 in plain english
HIH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic