-1

I am not new to programming but yet I always feel confused about some fundamental concepts about OOP. Currently I am developing using C#, and I wonder WHY and WHEN (or if it is not possible) to use the combination of the following?

  1. public class with private/protected member <---(Edited: this is dumb, ignore this)
  2. private/protected class with public member

Especially for 2. , I cannot think of any scenario which can apply to.

EDITED:

What I was asking is that, is there any reason or scenario that

public class A{
    private class B{
        public member x,y,z...
    }
}

instead of

public class A{
    private class B{
        private / protected member x,y,z...
    }
}

because my thought is like if one really wants to encapsulate class B, the member inside should not be public as well...

shole
  • 4,046
  • 2
  • 29
  • 69
  • possible duplicate of [Private class with Public method?](http://stackoverflow.com/questions/7777256/private-class-with-public-method) – Vlad Bezden Sep 18 '15 at 01:52
  • Classes can only be private or protected when they are nested inside other classes, otherwise they are either public or internal (internal is the default if not specified). If you think of it as a nested class, the private class is accessible to its owning type, and can access its public methods, it makes more sense. – Ron Beyer Sep 18 '15 at 02:01
  • @RonBeyer But in what case will happen like : public class A --> private class B --> public members ? Instead isn't use private / protected member inside B makes more sense? Am I mixing up something? – shole Sep 18 '15 at 02:04
  • @shole Only if you didn't want that method inside class B to be visible in class A, otherwise if you need to access it from class A, it needs to be marked public. A private classes private members are not visible by the owning type, if that makes sense. – Ron Beyer Sep 18 '15 at 02:11
  • @RonBeyer Yes that makes sense now :) – shole Sep 18 '15 at 02:14

2 Answers2

2

Lets take a look at a few examples, first the public class with private/protected members:

public class PublicClass
{
    public void PublicMethod() 
    {
        ProtectedMethod();      //Valid call
        PrivateMethod();        //Also valid
    }
    protected void ProtectedMethod()
    {
        PublicMethod();         //Valid
        PrivateMethod();        //Valid
    }
    private void PrivateMethod()
    {
        PublicMethod();         //Valid
        PrivateMethod();        //Valid
    }
}

public class SomeOtherClass
{
    public void SomeMethod()
    {
        PublicClass c = new PublicClass();
        c.PublicMethod();       //Valid
        c.ProtectedMethod();    //Invalid, not accessible
        c.PrivateMethod();      //Also invalid
    }
}

Here, all these class calls are valid internally, but PrivateMethod is not visible to anything outside the PublicClass, including any class that derives from PublicClass. ProtectedMethod is also hidden outside of the class but is still accessible to classes that inherit PublicClass, like this:

public class InheritedPublicClass : PublicClass
{
    public void MyPublicMethod()
    {
        PublicMethod();     //Calls base class method
        ProtectedMethod();  //Calls base class method
        PrivateMethod();    //Invalid, not accessible
    }
}

The call to PrivateMethod() is a compile time error because it is not accessible to the InheritedPublicClass.

Private allows you to create methods that are only visible to the immediate class, and nothing outside of it. Protected allows you to create methods that are visible internally, and visible to any class that derives from it.

Now lets look at a private class:

public class ParentClass
{
    private class NestedClass
    {
        public void PublicMethod() { }
        private void PrivateMethod() { }
    }

    private void SomeMethod()
    {
        //NestedClasscan only be created by methods of ParentClass
        var nc = new NestedClass();
        nc.PublicMethod();      //Internally visible
        nc.PrivateMethod();     //Invalid, not accessible
    }
}

Here NestedClass is private because it is nested inside ParentClass. It is not visible to anybody outside of NestedClass including inheritors. If we made it protected, then inheritors would be able to see it.

Again, NestedClass.PrivateMethod() is not visible outside of the implementation of the nested class.

This is useful for internal data structures that don't have any meaning outside of the class.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
0

The singleton pattern has some implementations that use a nested/inner private class.

The great Jon Skeet explains the evolution of a singleton pattern implementation from a not thread-safe variant all the way up to .NET 4.0's Lazy<T> implementation here.

The private class example from the above link, is this:

public sealed class Singleton
{
    private Singleton()
    {
    }

    public static Singleton Instance { get { return Nested.instance; } }

    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}
Karl Anderson
  • 34,606
  • 12
  • 65
  • 80