This article will discuss about Access Modifier.
Introduction
This article will discuss about Access Modifier. Watch our 500 videos on Azure, WCF, WPF, LINQ, Design Patterns, WWF, Silverlight, UML @ http://www.questpond.com/
What is Access Modifier?
Access modifiers determine the extent to which a variable or method can be accessed from another class or object
The following five accessibility levels can be specified using the access modifiers
- Private
- Protected
- Internal
- Protected internal
- Public
1. What is Private access modifier?
In OOP Attributes, Methods, Properties, Constructors declared with “private” access modifier get access or visible only to members of current class
For e.g.: Let’s take class BankDetail with access modifier “private”
class BankDetail
{
private string _employeename;
private int _accountnumber;
private double _balanceamount;
private string EmployeeName
{
set
{
_employeename = value;
}
get
{
return _employeename;
}
}
private int AccountNumer
{
set
{
_accountnumber = value;
}
get { return _accountnumber; }
}
private double BalanceAmount
{
set { _balanceamount = value; }
get { return _balanceamount; }
}
private void DisplayDetail()
{
Console.WriteLine("BankDetail for " + _employeename + "Account Number " + _accountnumber
+ " with Balance Amount is " + _balanceamount);
}
}
Create another class “clsShowDetails”
Now inside class “clsShowDetails” create object of class BankDetail
BankDetail objBank = new BankDetail();
Fron Object objBank if we are trying to access methods,properties of class BankDetail we cannot access them due to the protction level of “private” access modifier
public class clsShowDetails
{
BankDetail objBank = new BankDetail();
public void getDetails()
{
objBank.DisplayDetail();
}
}
From class “clsShowDetail”
public void getDetails()
{
objBank.DisplayDetail();
}
We will get error message
(BankDetail.DisplayDetail() is inaccessible due to protection level)
This means we cannot access Attributes, Methods, Properties, Constructors declared with “private” access modifier outside of class but can get access or visible only to members of current class
2. What is protected access modifier?
In OOP Attributes, Methods, Properties, Constructors declared with “protected” access modifier get access or visible only to members of current class as well as to members of derived class.
For e.g.: Let’s take class BankDetail with access modifier “protected”
class BankDetail
{
protected string _employeename;
protected int _accountnumber;
protected double _balanceamount;
protected string EmployeeName
{
set
{
_employeename = value;
}
get
{
return _employeename;
}
}
protected int AccountNumer
{
set
{
_accountnumber = value;
}
get { return _accountnumber; }
}
protected double BalanceAmount
{
set { _balanceamount = value; }
get { return _balanceamount; }
}
protected void DisplayDetail()
{
Console.WriteLine("BankDetail for " + _employeename + "Account Number " + _accountnumber
+ " with Balance Amount is " + _balanceamount);
}
}
Create another class “clsShowDetails”
Now inside class “clsShowDetails” create object of class BankDetail
BankDetail objBank = new BankDetail();
public class clsShowDetails:BankDetail
{
public void getDetails()
{
DisplayDetail();
}
}
From class “clsShowDetail”
public void getDetails()
{
DisplayDetail();
}
We will get no error message
This means we can access Attributes, Methods, Properties, Constructors declared with “protected” access modifier in derived class
3. What is internal access modifier?
In OOP Attributes, Methods, Properties, Constructors declared with “internal” access modifier get access or visible only to members of current project or in current namespace
For e.g.: Let’s take class BankDetail with access modifier “internal”
class BankDetail
{
internal string _employeename;
internal int _accountnumber;
internal double _balanceamount;
internal string EmployeeName
{
set
{
_employeename = value;
}
get
{
return _employeename;
}
}
internal int AccountNumer
{
set
{
_accountnumber = value;
}
get { return _accountnumber; }
}
internal double BalanceAmount
{
set { _balanceamount = value; }
get { return _balanceamount; }
}
internal void DisplayDetail()
{
Console.WriteLine("BankDetail for " + _employeename + "Account Number " + _accountnumber
+ " with Balance Amount is " + _balanceamount);
}
}
Add New Item in the same project

Create another class “clsShowDetails” in Class2.cs file
Now inside class “clsShowDetails” create object of class BankDetail
BankDetail objBank = new BankDetail();
public class clsShowDetails
{
BankDetail objBank = new BankDetail();
public void getDetails()
{
objBank.DisplayDetail();
}
}
From class “clsShowDetails”
public void getDetails()
{
DisplayDetail();
}
We will get no error message
This means we can access Attributes, Methods, Properties, and Constructors declared with “internal” access or visible only to members of current project or in current namespace
4. What is protected internal access modifier?
In OOP Attributes, Methods, Properties, Constructors declared with “protected internal” access modifier get access or visible only to members of current project or in current namespace and also to members of derived class
5. What is public access modifier?
In OOP Attributes, Methods, Properties, Constructors declared with “public” access modifier get access or visible to all members of classes and projects
class BankDetail
{
public string _employeename;
public int _accountnumber;
public double _balanceamount;
public string EmployeeName
{
set
{
_employeename = value;
}
get
{
return _employeename;
}
}
public int AccountNumer
{
set
{
_accountnumber = value;
}
get { return _accountnumber; }
}
public double BalanceAmount
{
set { _balanceamount = value; }
get { return _balanceamount; }
}
public void DisplayDetail()
{
Console.WriteLine("BankDetail for " + _employeename + "Account Number " + _accountnumber
+ " with Balance Amount is " + _balanceamount);
}
}
Create another class “clsShowDetails”
Now inside class “clsShowDetails” create object of class BankDetail
BankDetail objBank = new BankDetail();
public class clsShowDetails
{
BankDetail objBank = new BankDetail();
public void getDetails()
{
objBank.DisplayDetail();
}
}
From class “clsShowDetail”
public void getDetails()
{
objBank.DisplayDetail();
}
We will get no error message
Let’s try to access “public” access modifier with another example
Add New Item in the same project

Create another class “clsShowDetails”
Now inside class “clsShowDetails” create object of class BankDetail
BankDetail objBank = new BankDetail();
public class clsShowDetails
{
BankDetail objBank = new BankDetail();
public void getDetails()
{
objBank.DisplayDetail();
}
}
From class “clsShowDetail”
public void getDetails()
{
objBank.DisplayDetail();
}
We will get no error message
This means “public” access modifier is access or visible to all members of classes and projects.