C# Access modifiers/Specifiers for a class
Access modifier or Access specifier are the keywords of C# used to specify the accessibility to a type and it's members.
Discussion
Discussion
- What is C# Access Modifier for a class
- Types of Access Modifiers
- Use in real time scenario
- Few Interview Question Associated with it
Lets discuss one by one and Understand in deep.
A.What is C# Access Modifiers and its type?
- C# Modifiers is used to describe the accessibility for class and its members.
- This provides Security and also control the access level.
B.Types
Access Modifiers/Specifiers are of 5 types.
- Private
- Protected
- Internal
- Protected Internal
- Public
Private:- Private Access Specifier/Modifier used to make the class Private. But remember we can not declare class as private.
We can create a private class by using Private Constructor.
Private Access Modifier Or Private Access Specifier |
In the above Fig. we create a normal class called "Private-Class" and decorated the class with a private constructor. So the class will be considered as a private class.
Note:-
- The default access modifier of a constructor is Private.
- The default access modifier of a class variable and method are Private.
Protected:-Protected access modifier used to make the class accessible to only parent class (Inherited class).
Protected Access Modifier | Protected Access Specifier |
In the above Image we can see Child Class is inheriting from the parent class . So we can access the Protected members of the parent class .
Note:-
- It hide class method and class variable from outside class other than parent class.
- We can not declare Protected to a class as an access modifier or Access Specifier.
- Protected keyword can be declared to a Class variable and class member . i.e. Protected int my_int
Internal:-Internal make the class accessible to all the class which are associated with the current assembly i.e. in the same project.
Internal Access Modifier,Internal Access Specifier |
So the InternalClass1 is accessible outside of the class where as InternalClass2 is not accessible because the access modifier of the variable and method of a class is Private,But be noted that we may or may not declare a class to Internal as this is the default access modifier.
Note :-
1.The default access modifier of a class is Internal.
2.We may or may not declare Internal keyword before class . For example Internal Class My_class{}.
Protected Internal:- Protected Internal make the class accessible to the same assembly and the Inherited class/Parent class of other assembly.
It is the combination of both protected and Internal.
Lets take an example.
Lets create a class Library as below.
We can see we created a class Protected_Internal_Data and Created a method called Display_Data .
The access modifier of the method is Protected Internal .
Lets access this method from another project as below.
Protected Internal Access Modifier | Protected Internal Access Specifier |
We can see we can able to access the data of Display_Data() method in different assembly.
Note:-
1.We can not declare Protected Internal to a class ,but can declare to a method and its variable.
2.Widely used when we wanted to access the inherited class from other Project or Assembly.
Public:- Public makes the class accessible to all the class which are associated with the current assembly or outside the assembly.
So this is open to all the class and all the project.
We can declare public to a Class,method and variable as an access modifiers.
Look at this with an example.
Public Access Modifier | Public Access Specifier |
See the above Image.We declared Public to the class and class members and will try to access to another class .
See the below Image.
Public Access Modifier | Public Access Specifier |
In the above image we can able to access the other assembly class called clsPublic.
Conclusion:-
- Access modifier used for restrict the accessibility.
- Access modifier helps to secure your class and your class members.
- The default access modifier of a class is Internal.
- The default modifier of a method, variable and constructor are Private.
Thanks.....................
No comments:
Post a Comment