Access
Modifiers:
Access
Modifiers are nothing but the Usage and Limitation of our type like variable,
Methods and Class. For Example we can say it as a security limit.
private
public
internal
protected
protected
internal
This six are
the basic Access modifiers which we used in our C# Class/method and in
Variables.
Private:
Let’s take our
House Example .We will have a Security Guard in House, His duty is till the
Entrance of the house. He cannot go inside the house and access all the things.
In this case we can create a SecurityGuardClass and declare the variable and
method for Security as private .
Public:
House Owners
are public to the class where they can access all classes related to the House.
They have no restrictions to access their house.
Protected: Only the main class and derived class
can have access of protected variable or method. For example servants and
Guests are example for the Protected. For Example Servants can go to all room
and do cleaning and other activates. However, they have limitations of access
in the house, as they cannot take rest in a bed of house owner.
Internal: Access limit of variable or method
is within a project. For example let’s consider in our project we have more
than one class and we have declared a variable as internal in one class. Let’s
see an example program for internal variable.
Collapse | Copy Code
public class sampleInternalClass
{
internal String myInternal = "Iam
Internal Variable";
}
class ShanuHouseClass
{
int noOfTV = 2;
public String yourTVName = "SAMSUNG";
public void
myFirstMethod()
{
Console.WriteLine("You Have total " + noOfTV + "no of TV
:");
Console.WriteLine("Your TV
Name is :" + yourTVName);
}
static void Main(string[] args)
{
ShanuHouseClass objHouseOwner = new ShanuHouseClass();
objHouseOwner.myFirstMethod();
sampleInternalClass intObj = new sampleInternalClass();
Console.WriteLine("Internal Variable Example :" +
intObj.myInternal);
Console.ReadLine();
}
}
Protected Internal: Protected Internal variable or Method
has limitation with in a project of class or Derived class. Here is a sample
program for Protect Internal Variable .In this example I have used the
Inheritance .we will see in detail about Inheritance more detail in this
article.
Collapse | Copy Code
public class sampleProtectedInternalClass
{
protected internal String
myprotectedInternal = "Iam Protected Internal Variable";
public void
protectedInternalMethod()
{
Console.WriteLine("Protected Internal Variable Example :" +
myprotectedInternal);
}
}
public class derivedClass :
sampleProtectedInternalClass
{
public void
derivedprotectedInternal()
{
Console.WriteLine("Derived
Protected Internal Variable Example :" + myprotectedInternal);
}
}
class
ShanuHouseClass
{
int noOfTV = 2;
public String yourTVName = "SAMSUNG";
public void
myFirstMethod()
{
Console.WriteLine("You Have
total " + noOfTV + "no of TV :");
Console.WriteLine("Your TV Name is :" + yourTVName);
}
static void Main(string[] args)
{
ShanuHouseClass objHouseOwner = new ShanuHouseClass();
objHouseOwner.myFirstMethod();
sampleProtectedInternalClass intObj = new sampleProtectedInternalClass();
intObj.protectedInternalMethod();
derivedClass proIntObj = new derivedClass();
proIntObj.derivedprotectedInternal();
Console.ReadLine();
}
}
Note: The main and major things we need to
know in OOP concept are Encapsulation, Abstraction, Polymorphism and
Inheritance. We will discuss them in detail in this article.
No comments:
Post a Comment