GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — C# - Encapsulation
Selected Reading
C# - EncapsulationEncapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object oriented programming methodology, prevents access to implementation details. Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction. Encapsulation is implemented by using access specifiers. An access specifier defines the scope and visibility of a class member. C# supports the following access specifiers −
Public Access SpecifierPublic access specifier allows a class to expose its member variables and member functions to other functions and objects. Any public member can be accessed from outside the class. ExampleThe following example illustrates the use of a public access specifier in the rectangle class, which displays the area of the rectangle of the rectangle and its display − 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
Private Access SpecifierPrivate access specifier allows a class to hide its member variables and member functions from other functions and objects. Only functions of the same class can access its private members. Even an instance of a class cannot access its private members. ExampleIn the following example, we create a rectangle class in which we use the private member variable to hide the member variable − using System;
namespace RectangleApplication {
class Rectangle {
//member variables
private double length;
private double width;
public void Acceptdetails() {
Console.WriteLine("Enter Length: ");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("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 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 − Enter Length: 4.4 Enter Width: 3.3 Length: 4.4 Width: 3.3 Area: 14.52
Protected Access SpecifierProtected access specifier allows a child class to access the member variables and member functions of its base class. This way it helps in implementing inheritance. We will discuss this in more details in the inheritance chapter. ExampleIn this example, we create a rectangle class that calculates the area of a rectangle and we use the protected access specifiers for the length and width − using System;
namespace RectangleApplication {
class Rectangle {
// member variables
protected double length;
protected double width;
// Constructor to initialize length and width
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Method to calculate area
double GetArea() {
return length * width;
}
// Method to display rectangle details
public void Display() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle {
static void Main(string[] args) {
Rectangle r = new Rectangle(4.5, 3.5);
r.Display();
Console.ReadLine();
}
}
}
Following is the output of the above code − Length: 4.5 Width: 3.5 Area: 15.75 Internal Access SpecifierInternal access specifier allows a class to expose its member variables and member functions to other functions and objects in the current assembly. In other words, any member with internal access specifier can be accessed from any class or method defined within the application in which the member is defined. ExampleIn this example, we showcase how to encapsulate the properties and behaviors of a rectangle in an object-oriented manner using the internal access specifier − 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
Protected Internal Access SpecifierThe protected internal access specifier allows a class to hide its member variables and member functions from other class objects and functions, except a child class within the same application. This is also used while implementing inheritance. ExampleIn the following example, we create a rectangle class to calculate the area of the rectangle and use the protected internal access specifiers for the length and width member variable − using System;
namespace RectangleApplication {
class Rectangle {
// member variables
protected internal double length;
protected 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 preceding example, notice that the member function GetArea() is not declared with any access specifier. Then what would be the default access specifier of a class member if we don't mention any? It is private. Encapsulation Using Private Fields and MethodsYou can implement encapsulation by using the private fields and methods, which restrict direct access to the fields and can control data through using the public properties or methods. ExampleIn the following example, we are using private fields to encapsulate data and public methods ( using System;
class Person {
// Private field
private string name;
// Public method - Setting the name
public void SetName(string newName) {
if (!string.IsNullOrEmpty(newName)) {
name = newName;
} else {
Console.WriteLine("Name cannot be empty.");
}
}
// Public method - Getting the name
public string GetName() {
return name;
}
}
class Program {
static void Main() {
Person person = new Person();
person.SetName("Sudhir Sharma");
Console.WriteLine("Person's Name: " + person.GetName());
}
}
When the above code is compiled and executed, it produces the following result − Person's Name: Sudhir Sharma Encapsulation Using PropertiesThere is another way to implement encapsulation by using the properties (set and get). ExampleIn the following example, we are using a private field to store the salary and a public property ( using System;
class Employee {
private double salary; // Private field
// Public property with validation logic
public double Salary {
get { return salary; }
set {
if (value >= 3000) {
salary = value;
} else {
Console.WriteLine("Salary must be at least 3000.");
}
}
}
}
class Program {
static void Main() {
Employee emp = new Employee();
emp.Salary = 5000; // Valid salary
Console.WriteLine("Employee Salary: " + emp.Salary);
emp.Salary = 2000; // Invalid salary
Console.WriteLine("Updated Salary: " + emp.Salary);
}
}
When the above code is compiled and executed, it produces the following result − Employee Salary: 5000 Salary must be at least 3000. Updated Salary: 5000 Advertisements
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |