Scope
and Access Level :
A member be visible from
outside object and can be access from outside object as specified by
the Access Level.
Element of an application can be access according to the access
modifier defined on the element.
The following
are the access modifier
-
Public
-
Private
-
Protected
-
Internal
-
Protected internal.
The features
of access modifier listed below.
Public:
Public modifier are
visible to all code in the application.Can be access form anywhere in
the program. Member of the same type access any public modifier,
public type can be derived. Public type can be visible are inside the
same assembly only. Here is an example of public type .
Example 1
public
class
Student
{
int
i
= 56;
public
int
getData()
{
return
5;
}
}
Student
obj
= new
Student();
obj.getData();//access
to pubcli class
Example
2
public
struct
college
{
int
l
= 56;
int
k
= 70;
public
int
getData(int
a1,
int
a2)
{
l
= a1+a2;
return
l;
}
}
college
obj
= new
college();
obj.getData(10,20);//access
to pubcli struct
Private :
Private class are access only by the same member same member of a
type. Private type cannot be derived. No outside program can we
access to the private type modifier. Private type modifier cannot be
access from outside assembly. Here is the example of private type
modifier.
Example 1
private
class
Student
{
int
i
= 56;
private
getData()
{
return
5;
}
}
Student
obj
= new
Student();
obj.getData();//error
Example
2
private
struct
college
{
int
l
= 56;
int
k
= 70;
private
int
getData(int
a1,
int
a2)
{
l
= a1+a2;
return
l;
}
}
college
obj
= new
college();
obj.getData();//error
Protected:
Protected type modifier can we access from within the type define in
it and the type derived from it. Protected class can not be access
from outside the class rather than derived class. Protected class
cannot be access from outside the Assembly. Here is the example
of protected access modifier.
Example 1
protected
class
Student
{
int
i
= 56;
public
int
getData()
{
return
5;
}
}
protected
class
Student_child
:
Student
{
int
l
= 60;
public
int
getData_child()
{
return
5;
}
}
Student
obj
= new
Student();
obj.getData();//
Student_child
obj1
= new
Student_child();
obj1.getData_child();//
obj1.getData();//method
of student
Example 2
protected
struct
college
{
int
l
= 56;
int
k
= 70;
public
int
getData(int
a1,
int
a2)
{
l
= a1+a2;
return
l;
}
}
Internal:
Internal is access modifier ,which tells the compiler, that type will
be accessible from all within the Assembly but not accessible outside
the Assembly. Internal also be accessed from within the type define
in it. Here is the example of internal class.
Example 1
internal
class
Student
{
}
Protected
Internal : Protected internal access modifier can we access from
within the tribe drive in it, access form derived class and visible
within the Assembly only.
protected
class
Student
{
protected
internal
string
Name;
protected
internal
decimal
price;
}
Student
obj
= new
Student();
obj.Name
= "John";
obj.price
= 20;
Here are some
interview question.
1)Can you
declare a private class in a namespace?
A
class in a namespace are by default internal, that means that class
can be visible could assembly within this namespace. The class can be
declared public only not private protected internal. It will throw
error. But if we are declaring a class with nested type, the nested
type can be declared as private class.
2)What are
the default access modifier of a class?
All
class defined in a. Net application or internal by default.
No comments:
Post a Comment