title: Public, private and protected access qualifiers for D classes
body: <p>I'm a C++ programmer starting with D and I'm having some trouble understanding the access qualifiers for D classes. Consider the following example:</p>
  
  <pre><code>import std.stdio;
  
  class Foo {
  
      private void aPrivateMethod()
      {
          writeln("called aPrivateMethod");
      }
  
      protected void aProtectedMethod()
      {
          writeln("called aProtectedMethod");
      }
  
      public void aPublicMethod()
      {
          this.aPrivateMethod();
          this.aProtectedMethod();
      }
  }
  
  void main(string[] args)
  {
      Foo foo = new Foo();
  
      foo.aPublicMethod();    // OK to call it from anywhere
      foo.aPrivateMethod();   // Must not be allowed to call it outside Foo
      foo.aProtectedMethod(); // Should only be callable from within Foo and derived classes
  }
  </code></pre>
  
  <p>I would expect the previous code to fail compilation, since it is calling private and protected methods of class <code>Foo</code> in an external function. However, this is not the case, since the example above compiles and runs without errors or warnings on DMD v2.063.2. Clearly the keywords have different meaning from those of C++.</p>
  
  <p>My questions are: </p>
  
  <p>1) How to make a method and/or variable private to a class so that only the class in question can access it.</p>
  
  <p>2) How to make a method and/or variable protected, so that only the class in question and its derived classes can access it.</p>