title: Performance vs Design related to private/protected methods in java
body: <p>We're having a discussion with a friend concerning java code design and efficiency. </p>
  
  <p>He argues methods should preferably be private for performance reasons as well as class consistency protection when overriding. </p>
  
  <p>I argue methods should preferably be protected for complete customizability, and to avoid loosing time modifying and releasing an API as soon as its user want to change part of its behaviour.</p>
  
  <p>We know the preference for <a href="https://stackoverflow.com/questions/4913025/reasons-to-use-private-instead-of-protected-for-fields-and-methods">composition over inheritance</a> so here I mainly focus on performance comparison.</p>
  
  <p>A simple test (see below) proves extended classes with parent having protected methods aren't slower than those with parent having private methods. It's even <em>sometimes</em>  (I don't really understand performance change) faster.</p>
  
  <pre><code>elapsed:8051733.063 microseconds for A (private)
  elapsed:8036953.805 microseconds for B (protected)
  </code></pre>
  
  <p>Do you think the below mentionned test is robust enough for making the comparison?</p>
  
  <pre><code>public class VerifPerfProtected {
  public static void main(String[] args) {
      int ncalls = 1000000000; //10^9
      ChildrenClassA a = new ChildrenClassA();
      ChildrenClassB b = new ChildrenClassB();
  
      long start = System.nanoTime();
      a.manyCalls(ncalls);
      long stop = System.nanoTime();
      System.out.println("elapsed:" + (stop - start)/1000.0 + " microseconds for A (private)");
  
      start = System.nanoTime();
      b.manyCalls(ncalls);
      stop = System.nanoTime();
      System.out.println("elapsed:" + (stop - start)/1000.0 + " microseconds for B (protected)");
  }
  
  public static class ParentClassA{
      public void manyCalls(int n){
          for (int i = 0; i &lt; n; i++) {
              callAmethod();
          }
      }
      public void callAmethod(){
          aPrivateMethod();
      }
      private void aPrivateMethod(){
          int a=0;
          for (int i = 0; i &lt; 5; i++) {
              a++;
          }
      }
  }
  
  public static class ParentClassB{
      public void manyCalls(int n){
          for (int i = 0; i &lt; n; i++) {
              callAmethod();
          }
      }
      public void callAmethod(){
          aProtectedMethod();
      }
      protected void aProtectedMethod(){
          int a=0;
          for (int i = 0; i &lt; 5; i++) {
              a++;
          }
      }
  }
  
  public static class ChildrenClassA extends ParentClassA{        
  }
  
  public static class ChildrenClassB extends ParentClassB{        
  }
  }
  </code></pre>