Encapsulation
In an
Object Oriented programming language, encapsulation is one of the key language
features. Encapsulation is the process of encapsulating data and functions into
a single unit. Best example of Encapsulation is Class.
Encapsulation
provides a technique to protect data from accidental corruption. Rather than
defining the data in the form of public, we can declare those fields as
private. The Private data are manipulated indirectly by two ways. Let us see
some example programs in C# to demonstrate Encapsulation by those two methods.
The first method is using a pair of conventional accessor and mutator methods.
Another one method is using a named property. Whatever be the method our aim is
to use the data without any damage or change.
Encapsulation is achieved
by using access specifiers. An access specifier defines
the scope and visibility of a class member. C# supports the below access
specifiers −
- Public
- Private
- Protected
- Internal
- Protected internal
· We can choose any of these to protect our data. Public is not restricted and Private is most restricted. The following table describes about the accessibility of each.
Access Specifier |
Description |
Public |
It specifies that access is not restricted. |
Protected |
It specifies that access is limited to the containing class or in
derived class. |
Internal |
It specifies that access is limited to the current assembly. |
protected internal |
It specifies that access is limited to the current assembly or types derived from the containing class. |
Private |
It specifies that access is limited to the containing type. |
Public Access Specifier
It allows
a class to expose its member variables and member functions to other functions
and objects.
The following example
illustrates this −
using System;
namespace EncapsulationApplication {
class Test {
//member variables
public double length;
public double width;
public double Area() {
return length * width;
}
public void Show() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", Area());
}
}//end class Test
class TestDemo {
static void Main(string[] args) {
Test r = new Test();
r.length = 4.5;
r.width = 3.5;
r.Show();
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, the member variables length and width are declared public,
so they can be accessed from the function Main() using an instance of the
Rectangle class, named r.
The member
function Show() and Area() can also access
these variables directly without using any instance of the class.
The member
functions Show() is also declared public, so it
can also be accessed from Main() using an instance of the
Rectangle class, named r.
Private Access Specifier
Private
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.
.
The following example
illustrates this −
using System;
namespace EncapsulationApplication {
class Test {
//member variables
private double length;
private double width;
public void TakeDetails() {
Console.WriteLine("Enter Length: ");
length = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter Width: ");
width = Convert.ToDouble(Console.ReadLine());
}
public double Area() {
return length * width;
}
public void Show() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Test
class TestDemo {
static void Main(string[] args) {
Test r = new Test();
r.TakeDetails ();
r.Show();
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
In the preceding
example, the member variables length and width are declared private,
so they cannot be accessed from the function Main(). The member functions TakeDetails() and Show() can
access these variables. Since the member functions TakeDetails() and Show() are
declared public, they can be accessed from Main() using
an instance of the Rectangle class, named r.
Protected Access Specifier
Protected 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.
Internal Access Specifier
Internal
access specifier allows a class to expose its member variables and member
functions to other functions and objects in the current assembly.
The following program
illustrates this −
using System;
namespace EncapsulationApplication {
class Test {
//member variables
internal double length;
internal double width;
double Area() {
return length * width;
}
public void Show() {
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle
class TestDemo {
static void Main(string[] args) {
Test r = new Test();
r.length = 4.5;
r.width = 3.5;
r.Show();
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 Area() 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.
Protected Internal Access Specifier
The 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.
Very nice article. Very useful
ReplyDelete