YouTip LogoYouTip

Csharp Encapsulation

**** **Content:** **Encapsulation** is defined as "wrapping one or more items into a single unit." In object-oriented programming, encapsulation is used to prevent access to implementation details. Abstraction and encapsulation are related features of object-oriented programming. Abstraction allows relevant information to be visualized, while encapsulation enables developers _to implement the desired level of abstraction_. C# encapsulation uses **access modifiers** to set the access level for users as needed. An **access modifier** defines the scope and visibility of a class member. The access modifiers supported in C# are as follows: * public: All objects can access; * private: The object itself can access it within the object; * protected: Only objects of that class and its subclasses can access; * internal: Objects within the same assembly can access; * protected internal: Access is limited to the current assembly or types derived from the containing class. !(#) 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 ```csharp 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 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 example above, the member variables `length` and `width` are declared as **public**, so they can be accessed by the function `Main()` using an instance **r** of the `Rectangle` class. The member functions `Display()` and `GetArea()` can directly access these variables. The member function `Display()` is also declared as **public**, so it can also be accessed by `Main()` using the instance **r** of the `Rectangle` class. The private access modifier allows a class to hide its member variables and member functions from other functions and objects. Only functions within the same class can access its private members. Even an instance of the class cannot access its private members. The following example illustrates this: ## Example ```csharp using System; namespace RectangleApplication { class Rectangle { // private member variables private double length; private double width; // public method to get length and width from user input public void AcceptDetails() { Console.WriteLine("Enter Length:"); length = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Enter Width:"); width = Convert.ToDouble(Console.ReadLine()); } // public method to calculate area public double GetArea() { return length * width; } // public method to display properties and area 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) { // Create an instance of the Rectangle class Rectangle r = new Rectangle(); // Get length and width from user input via the public method AcceptDetails() r.AcceptDetails(); // Display the rectangle's properties and area via the public method Display() r.Display(); Console.ReadLine(); } } } When the above code is compiled and executed, it produces the following result: Enter Length: 5 Enter Width: 3 Length: 5 Width: 3 Area: 15 **Explanation:** * `length` and `width` are declared as private member variables to prevent direct access and modification from outside the class. * The `AcceptDetails` method allows the user to input the rectangle's length and width, which is an example of using a public method to operate on private variables. * The `GetArea` method is used to calculate the area of the rectangle, while the `Display` method is used to show the rectangle's properties and area. * In the `ExecuteRectangle` class, operations are performed by creating an instance of the `Rectangle` class and then calling its public methods. This way, the main program cannot directly access and modify the rectangle's length and width, but instead operates through the public interface provided by the class, achieving encapsulation. The protected access modifier allows subclasses 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. The internal access modifier allows a class to expose its member variables and member functions to other functions and objects within the current program. In other words, any member with an internal access modifier can be accessed by any class or method defined within the application where that member is defined. The following example illustrates this: ## Example ```csharp 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 example above, 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 used, which is **private**. The protected internal access modifier allows access within the same class, derived classes, or within the assembly containing the class. This is also used to implement inheritance.
← Csharp MethodsCsharp Continue Statement β†’