Search code examples
javaoverridingprivatefinal

Why are private methods in Java implicitly final?


I wanted to point this lines in the book "Java in a Nutshell" which caused me some confusion:

private methods are not inherited by subclasses and, therefore, cannot be overridden (i.e., all private methods are implicitly final).

I interpreted that all private methods are final, and I understand that:

  • Final methods cannot be overriden, but they can be called from a subclass.
  • Private methods cannot be overriden, and they cannot be called from a subclass.

Are my statements correct?


Solution

  • Final methods cannot be overriden, but they can be called from a subclass.

    This may or may not be true, depending on the modifier, and where your overriding class is. If the method is private or default (no modifier) then you cannot call the method from a subclass, unless your subclass is in the same package.

    Private methods cannot be overriden, and they cannot be called from a subclass.

    True. The reason private methods cannot be overridden, is because they can't be seen. This does not imply that methods that cannot be overridden are private however.