Access specifiers

Access specifier defines the scope of a class member. We generally use these access specifiers to give complete security to the data . We use different type of access specifiers to specify the extent of visibility of a class member. In C# we use totally 5 types of access specifiers

  1. public
  2. private
  3. protected
  4. internal
  5. protected internal
  • public access specifier
    1. This access specifier allows the member data and functions of a class to be exposed to other class functions and objects .
    2. The member which is declared as public can be accessed from outside the class.
example:

using System;

class Car
{
private string Car_color;
}
class Bike
{
public string bike_color;
}
class Result
{
static void Main(string [] arg)
{
Car Camry=new Car();
Bike unicorn=new Bike():

Camry.Car_color="white"; //.................................. Error..! cant access private var outside class.
unicorn.bike_color="black"; //................................assigns black to variable.
Console.ReadLine();
}
}

example 2:
using System;
class Car
{
public string color;
public void Honk()
{
Console.WriteLine(" Member function invoked");
}
}
class Result
{
static void Main(string [] arg)
{
car Camry =new Car();
Camry.Honk();//.............. displays the msg.........................Member function invoked.
Console.ReadLine();
}
}

  • private access specifier
    1. This access specifier allows the private data of a class to be hidden from out side classes.
    2. Only the members of the same class r provided permissions to access the private members.
example:

using System;

class Car
{
private string Model;

void Honk()
{
Console.WriteLine("beep beep....!");
}
public void setModel
{
Console.WriteLine("Enter the model num:\t");
Model=Console.ReadLine();
}
public void Display()
{
Console.WriteLine("The model is :\t");
}
}
class Result
{
static int Main(string [] arg)
{
Car Camry=new Car();

Camry.setModel(); //..................Prompts user to enter the model..
Camry.Display();//...............Displays the model.
Camry.Honk();// ................. can not access private methods out side class.
Console.WriteLine(Camry.Model);//...............can not access private mem outside class.

return 0;
Console.ReadLine();
}
}


Note:
When u do not specify any data members as either public, private, protected then the default access specifier is private.
  • Protected access specifier:
    1. This specifier allows a class to expose its member data and functions only to its child classes .
    2. It hides the data to be accessed from other class objects and functions.
example:

using System;
class Car
{
protected string model;
void Method()
{
Console.WriteLine("Member function invoked");
}
public void setModel()
{
Console.WriteLine("Enter the model name :\t");
model=Console.ReadLine();
}
void Display()
{
Console.WriteLine("The model is :\t");
}
}
class Result
{
static int Main(string [] arg)
{
Car civic =new Car();
civic.Method();//......... Error!...can not access this member as it is a protected member.
civic.setModel();//....... accepts the input.
civic.Display();//.......Error!...private member can not be accessed.
Console.WriteLine(civic.model);//.....protected members can not be accessed.

return 0;
Console.ReadLine();
}
}
  • internal access specifier:
    1. Any member that is declared internal can be accessed from any class or method defined within an application in which the member is defined.
    2. When u do not specify any class class as either public, private, protected then the default access specifier for a class is internal.
example:

using System;
class Bike
{
private string col;

internal void Method()
{
Console.WriteLine("Method invoked");
}
}
class Result
{
static void Main(string[] arg)
{
Bike Fz=new Bike();
Console.WriteLine(Fz.col);//...........Error!...can not access private data.
Fz.Method();//............Displays method invoked.
Console.ReadLine();
}
}

Note:
the main difference between public and internal is
  1. public is visible to objects of other class outside the namespace collection and also to the child classes outside the namespace collection.
  2. But internal access specifier is not visible to objects of other class outside the namespace collection and also to the child classes outside the namespace collection.
  • protected internal access specifier:
    1. This specifier allows a class to hide its mem var, mem funct, to be accessed from other class objects and functions, except the child class ,within the application.
    2. This access specifier ecomes important while implementing inheritance.
example:

using System;

class Car
{
protected internal string col;
void Method()
{
Console.WriteLine(" Method invoked");
}
}
class Result
{
static int Main(string [] arg)
{
Car audi =mew Car();
audi.Method();//........Error!... private function.
Console.WriteLine(audi.col);//...... cannot access protected internal members outside the class .

return 0;
Console.ReadLine();
}
}
  1. this Acc.Spec has the similar properties as that of internal and the only dfference is that it can not be visible to objects of other class within the same namespace.






0 Comments:

Post a Comment



Newer Post Older Post Home