title: Level of encapsulation in JAVA
body: <p>I am working with a project and don't want to give access to the innermost classes. This is the class structure I am using,</p>
  
  <pre><code>// data types are not important unless mentioned
  class One
  { 
      protected propertyOne;
      protected String id; 
  }
  
  class Two
  {
      protected ArrayList&lt;One&gt; arrayOne;
      protected String id;
  }
  
  class Three
  {
      protected ArrayList&lt;Two&gt; arrayTwo;
      protected otherProperty;
  }
  </code></pre>
  
  <p>Now, main class will instantiate Three and should use Two and One through member methods. If I return any of <code>ArrayList</code> through get function, all the data can be easily changed from anywhere. So instead of <code>ArrayList&lt;Two&gt;</code>, I am returning <code>ArrayList&lt;Two.id&gt;</code> and then another method is called with particular <code>Two.id</code>. This takes care of keeping <code>ArrayList</code>s hidden but generates lots of code and extra objects because of <code>ArrayList&lt;Two.id&gt;</code> and <code>ArrayList&lt;One.id&gt;</code>.</p>
  
  <p>In C++, I would have made a <code>const</code> method with <code>const</code> return type to ensure no changes in <code>ArrayList</code>. </p>
  
  <p>Is there any smart way in Java to keep <code>ArrayList</code>s hidden?</p>