What is Access Specifiers?

jayalaxmi By jayalaxmi, 24th Oct 2014 | Follow this author | RSS Feed | Short URL http://nut.bz/2rva5c76/
Posted in Wikinut>Jobs>Training

difference between access modifiers and when we use specific access modifer

Access Specifier

These are also modifiers used to define scope of a type (Class or Interface or Structure) as well as their members i.e. who can access them and who cannot.
C# has 5 different Specifiers those are
1.) Private
2.) Internal
3.) Protected
4.) Protected Internal
5.) Public

1.) Private

Members declared as private under class or structure is not accessible outside of them in which they are defining. Default scope for members of a class and structure is private, but public in the case of interface. Interface cannot contain private member in it. A type can never be declared as private.

2.) Protected

Members declared as protected under a class or interface can be accessed only from its children or within itself , an non – child class cannot consumed .Types cannot be declared as protected also.

Q – How to restrict a class not to be accessible from any other class?
Ans – This can be done by declaring constructors of classes private
Q – How to restrict a class not to be inherited for any other class?
Ans – This can be done by declaring class as Sealed.
Q – How to restrict a class not to be accessible for any other class to consume by creating its object?
Ans – This can be done by declaring constructor of a class is Private.

3.) Internal

Members of types that are declared as internal are accessible only within the project both from child or non-child class the default Scope for only type in C# is internal only.

4.) Protected Internal

Members declared as Protected Internal enjoy dual scope i.e. within the project they behave as Internal providing access to others, outside the project they change protect and still provide access to child classes.

5.) Public

Public – A type or member of a type if declared as Public is global in scope that can be access from anywhere.
Cases Private Internal Protected Protected Internal Public
case 1 w w w w w
case 2 X w w w w
case 3 X w X w w
case 4 X X w w w
case 5 X X X X w

w means Applicable
X means not Applicable

Tags

Access Modifier, Access Specifier, Internal, Private, Protected, Public, What Is Access Specifier

Meet the author

author avatar jayalaxmi
myself jayalaxmi from bangalore and i'm a software engineer i like to share my knowledge with other.

Share this page

moderator Steve Kinsman moderated this page.
If you have any complaints about this content, please let us know

Comments

author avatar AjaySinghChauhan
24th Oct 2014 (#)

very nice post and thanks for sharing it with us

Reply to this comment

Add a comment
Username
Can't login?
Password