ACCESS MODIFIERS
Access
modifiers are an integral part of object-oriented programming. They support the
concept of encapsulation, which promotes the idea of hiding functionality.
Access modifiers allow you to define who does or doesn't have access to certain
features.
In C# there are 3 different types of Access
Modifiers.
Modifier
|
Description
|
public
|
There are no restrictions on accessing public members.
|
Private
|
Access is limited to within the class definition. This is the
default access modifier type if none is formally specified
|
protected
|
Access is limited to within the class definition and any class
that inherits from the class
|
INTERNAL
|
|
PROTECTEDINTERNAL
|
1. PUBLIC
The public
keyword is an access modifier for types and type members. Public access is the
most permissive access level.
There are no
restrictions on accessing public members.
Accessibility:
- Can
be accessed by objects of the class
- Can
be accessed by derived classes
EXAMPLE
PRIVATE
Private access is
the least permissive access level.
Private members
are accessible only within the body of the class.
Accessibility:
- Cannot
be accessed by object
- It
Can be access with in the class only, with in it’s methods of that class
- Cannot
be accessed by derived classes
Example:
PROTECTED
A protected
member is accessible from within the class in which it is declared, and from
within any class derived from the class that declared this member.
A protected
member of a base class is accessible in a derived class only if the access
takes place through the derived class type.
Accessibility:
- Cannot
be accessed by object
- By
derived classes
EXAMPLES
CODING
IN CLASS:
namespace
WindowsFormsApplication2
{
class Class1
{
protected
int sum=234;
}
class class2 : Class1
{
public void sub()
{
int
a=54;
int
b=65;
int
c=a+b;
System.Windows.Forms.MessageBox.Show(c.ToString());
System.Windows.Forms.MessageBox.Show(sum.ToString());
}
}
}
CODING ON BUTTON:
private
void button1_Click(object
sender, EventArgs e)
{
class2
ob = new class2();
ob.sub();
}
·
EVENTS IN WINDOW ARE PRIVATE
·
EVENTS IN WEB ARE PROTECTETED
·
IN WEB CLASS ARE PUBLIC
·
IN WINDOW CLASSES ARE INTERNAL
No comments:
Post a Comment