C# Encapsulation
Encapsulation is defined as "enclosing one or more items within a physical or logical package." In object-oriented programming methodology, encapsulation is used to prevent access to implementation details.
Abstraction and encapsulation are related features in object-oriented programming. Abstraction allows making relevant information visible, and encapsulation enables the developer to implement the desired level of abstraction.
C# encapsulation sets the access permissions of the user as needed and is achieved through access modifiers.
An access modifier defines the scope and visibility of a class member. C# supports the following access modifiers:
- public: Accessible by all objects.
- private: Accessible only within the class.
- protected: Accessible within the class and its derived classes.
- internal: Accessible within the same assembly.
- protected internal: Access is limited to the current assembly or types derived from the containing class.
Public Access Modifier
The public access modifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed by external classes.
The following example illustrates this:
Example
using System;
namespace RectangleApplication
{
class Rectangle
{
// Member variables
public double length;
public double width;
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
} // End of Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result:
Length: 4.5
Width: 3.5
Area: 15.75
In the above example, the member variables length and width are declared as public, so they can be accessed by the function Main() using an instance of the Rectangle class, r.
The member functions Display() and GetArea() can directly access these variables.
The member function Display() is also declared as public, so it can be accessed by Main() using an instance of the Rectangle class, r.
Private Access Modifier
The private access modifier allows a class to hide its member variables and member functions from other functions and objects. Only functions within the class can access its private members. Even an instance of the class cannot access its private members.
The following example illustrates this:
Example
using System;
namespace RectangleApplication
{
class Rectangle
{
// Member variables
private double length;
private double width;
public void Acceptdetails()
{
Console.WriteLine("Please enter length:");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter width:");
width = Convert.ToDouble(Console.ReadLine());
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
} // End of Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
} // end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result:
Please enter length:
4.4
Please enter width:
3.3
Length: 4.4
Width: 3.3
Area: 14.52
In the above example, the member variables length and width are declared as private, so they cannot be accessed by the Main() function.
The member functions AcceptDetails() and Display() can access these variables.
Since the member functions AcceptDetails() and Display() are declared as public, they can be accessed by the Main() function using the instance r of the Rectangle class.
Protected Access Modifier
The Protected access modifier allows a subclass to access the member variables and member functions of its base class. This helps in implementing inheritance. We will discuss this in detail in the inheritance chapter.
Internal Access Modifier
The Internal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current program. In other words, any member with the internal access modifier can be accessed by any class or method defined within the application in which the member is defined.
The following example illustrates this:
Example
using System;
namespace RectangleApplication
{
class Rectangle
{
// Member variables
internal double length;
internal double width;
double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
} // end class Rectangle
class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.length = 4.5;
r.width = 3.5;
r.Display();
Console.ReadLine();
}
}
}
When the above code is compiled and executed, it produces the following result:
Length: 4.5
Width: 3.5
Area: 15.75
In the above example, note that the member function GetArea() is declared without any access modifier. If no access modifier is specified, the default access modifier for a class member is private.
Protected Internal Access Modifier
The Protected Internal access modifier allows access within the class, derived classes, or within the same assembly. This is also used for implementing inheritance.