You must Sign In to post a response.
  • Category: .NET

    What are the access specifier can be used in class

    Hai All,

    May i know what are the access specifier can be used to create a class

    Default access specifier of class is internal

    we can change as public, but my doubt is ... when i change class access specifier to protected,private,protected internal it gives error

    Thanks and Regards,
    A.L.Chellappan
  • #769045
    Hi,

    Changing the access modifier of a method in a derived type is pointless that's why it's not allowing to do so.


    class Base
    {
    public virtual void A() {}
    }

    class Derived: Base
    {
    protected override void A() {}
    }


    Now here if you use protected,

    public class A
    {
    protected int x;

    public class B
    {
    public static A CreateClassA()
    {
    A x = new A();
    x.x = 5; // ERROR : No privilege
    return x;
    }
    }
    }

    Thanks,
    Mani

  • #769048

    Hai Chellappan,
    The default access specifier for the class is internal and we can make it public also. Only these two access specifiers will work for the main class. You can't make the main class or the outer class as private, protected or protected-internal etc.
    But you can use other access specifiers with the internal or nested classes. Like below:

    internal class A
    {
    protected class B
    {
    // some code
    }
    }

    Again another point is that if the outer class scope is internal(means it can be access in the same assembly/project), the inner class cant be the public(bigger scope than outer class). So if the outer class is public, you can use any access specifier for the internal class(i.e. public, internal, protected etc).
    Hope it will be helpful to you.


    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #769049
    Following access modifiers can be used in class
    public
    The type or member can be accessed by any other code in the same assembly or another assembly that references it.

    private
    The type or member can be accessed only by code in the same class or struct.

    protected
    The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

    internal
    The type or member can be accessed by any code in the same assembly, but not from another assembly.

    protected internal
    The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #769080
    Access specifiers are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Example of Access specifiers is given in this code snippet
     sing namespace std;

    class Line {
    public:
    double length;
    void setLength( double len );
    double getLength( void );
    };
    double Line::getLength(void) {
    return length ;
    }

    void Line::setLength( double len ) {
    length = len;
    }


Sign In to post your comments