C # package
Encapsulation is defined as “enclosing one or more projects in a physical or logical package”. In object-oriented programming methodology, encapsulation is designed to prevent access to implementation details.
Abstraction and encapsulation are related features of object-oriented programming. Abstraction allows visualization of related information, while encapsulation enables developers to achieve the required level of abstraction.
According to the specific needs, C# encapsulation sets the access rights of users, and realizes it through access modifiers.
An access modifier defines the scope and visibility of a class member. The access modifiers supported by C# are as follows:
Public: all objects are accessible;
Private: the object itself can be accessed inside the object;
Protected: only this class object and its subclass objects can access
Internal: Objects in the same assembly can be accessed;
Protected internal: access is limited to the current assembly or a type derived from the containing class.
Public access modifier
Public
access modifiers allow a class to expose its member variables andmember functions to other functions and objects. Any public member can beaccessed by an external class.
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());
}
}// Rectangle end
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 results:
Length: 4.5
Width: 3.5
Area: 15.75
In the above example, the member variable length
and width
are declared as public
, so they cannot be used by functions Main()
accessing instances of the Rectangle
class using r
.
Member function Display()
and GetArea()
, you can access these variables directly.
Member function Display()
is also declared to be public
, so it canalso be accessed by r
instances of the Rectangle
class using Main()
.
Private access modifier
Private
access modifiers allow a class to hide its member variables and member functions from other functions and objects. Only functions in the same class can access its private members. Even an instance of a 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 the length:");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Please enter the 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 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 results:
Please enter the length:
four point four
Please enter the width:
three point three
Length: 4.4
Width: 3.3
Area: 14.52
In the above example, the member variable length
and width
are declared as private
, so they cannot be used by functions Main()
visit.
Member function AcceptDetails()
and Display()
, which you can access thesevariables.
Because of the member function AcceptDetails()
and Display()
are declared as public
, so they can be accessed by r
instances of the Rectangle
class using Main()
.
Protected access modifier
The Protected access modifier allows a subclass to access member variables and member functions of its base class. This helps to implement inheritance.We will discuss this in detail in the chapter on inheritance. Discuss this in more detail.
Internal access modifier
Internal
access specifiers allow a class to expose its member variables and member functions to other functions and objects in the current program. In other words, with internal
any member of the access modifier can be accessed by any class or method within the application defined by that member.
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 results:
Length: 4.5
Width: 3.5
Area: 15.75
In the example above, notice the member function GetArea()
declared without any access modifiers. If no access modifier is specified, the default access modifier for class members is used, that is, private
.
Protected Internal access modifier
Protected Internal
access modifiers are allowed to be accessed in this class, derived classes, or assemblies that contain the class. This is also used to implement inheritance.