locked
Why Class can not be declared as Protected? RRS feed

  • Question

  • Why Class can not be declared as Protected?
    Thanks!! MCP
    Friday, May 21, 2010 4:00 PM

Answers

  • There are only two valid declarations for a class at the namespace level, "Internal" and "Public".

    The keyword, protected , is reserved for use with class members.  It must be used INSIDE of a class.


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by GurunGuns Sunday, May 23, 2010 5:08 PM
    Friday, May 21, 2010 5:24 PM
  • Actually not only Propetected you also can't declare a class as private.

    But there are other keywords which can help you achieve the same what propetcted does (i.e become available only upon inheritance)

    Only a nested type/class can be either propetcted or private.

    This is by design of the language. There is an abstract keyword which helps your class to be only inherited.


    Vidya Vrat Agarwal. MCPD,MCTS, MCT, MCSD .NET, MCAD .NET, MCSD. http://dotnetpassion.blogspot,com
    • Proposed as answer by Rudedog2 Friday, May 21, 2010 8:05 PM
    • Marked as answer by GurunGuns Sunday, May 23, 2010 5:08 PM
    Friday, May 21, 2010 8:03 PM

All replies

  • Actually, you can.  Although it is not what you are asking.  It can be done with nested classes.


        public class OuterClass
        {
            protected class NestedClass
            {
            }
        }

    You cannot declare protected classes at the namespace level because it would be meaningless.  Think about what the keyword "protected" means.  It applies to members of a class.  Protected members can be accessed only by the defining the class and any inheriting classes.

    Is it possible to inherit a namespace?

     


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, May 21, 2010 4:36 PM
  • Well, a class can be declared protected (in C# at least), what's the specific situation?

    Cristian.

    Friday, May 21, 2010 4:40 PM
  • There are only two valid declarations for a class at the namespace level, "Internal" and "Public".

    The keyword, protected , is reserved for use with class members.  It must be used INSIDE of a class.


    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by GurunGuns Sunday, May 23, 2010 5:08 PM
    Friday, May 21, 2010 5:24 PM
  • For a non-nested class, the existing "public abstract" declaration has the same meaning as a potential "protected" declaration, so there is no reason to allow "protected" for a non-nested class.
    Friday, May 21, 2010 5:52 PM
  • For a non-nested class, the existing "public abstract" declaration has the same meaning as a potential "protected" declaration, so there is no reason to allow "protected" for a non-nested class.


    No it doesn't.   " public abstract "

    For one, 'protected' members are not accessible to classes outside of the hierarchy. 

    The keyword 'public' means just the opposite.  Anyone can access it.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, May 21, 2010 6:22 PM
  • There is no class hierarchy until you create a subclass.  For a non-nested class, "public abstract" simply means that it may be subclassed from either within or outside its parent assembly.  If there were a "protected" declaration for non-nested classes, how would you imagine its behaviour might differ from this?
    Friday, May 21, 2010 7:07 PM
  • A protected declaration at the namespace level is meaningless.  Protected for, or from, what?  Access by other namespaces?  That is what 'internal' does.

    A namespace has no class hierarchy.  Can you inherit from a namespace? No.

     


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, May 21, 2010 7:15 PM
  • "Internal" keyword does not protect a class from been accessed by other namespaces but by other assembly... another namespace within the same assembly can access the class
    Friday, May 21, 2010 7:24 PM
  • "Internal" keyword does not protect a class from been accessed by other namespaces but by other assembly... another namespace within the same assembly can access the class

    Right.  What Raevean said.  Assemblies, not namepaces.

    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, May 21, 2010 7:36 PM
  • There is no class hierarchy until you create a subclass.  For a non-nested class, "public abstract" simply means that it may be subclassed from either within or outside its parent assembly.  If there were a "protected" declaration for non-nested classes, how would you imagine its behaviour might differ from this?


    I give up. I have already explained it enough times already.


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, May 21, 2010 7:41 PM
  • Actually not only Propetected you also can't declare a class as private.

    But there are other keywords which can help you achieve the same what propetcted does (i.e become available only upon inheritance)

    Only a nested type/class can be either propetcted or private.

    This is by design of the language. There is an abstract keyword which helps your class to be only inherited.


    Vidya Vrat Agarwal. MCPD,MCTS, MCT, MCSD .NET, MCAD .NET, MCSD. http://dotnetpassion.blogspot,com
    • Proposed as answer by Rudedog2 Friday, May 21, 2010 8:05 PM
    • Marked as answer by GurunGuns Sunday, May 23, 2010 5:08 PM
    Friday, May 21, 2010 8:03 PM
  • actually you can make it protected. You can make a base class where derive from.

    protected class BaseClass
    {
    
    }
    
    public class MyChildClass : BaseClass
    {
    
    }
    Friday, May 21, 2010 9:29 PM