Tuesday, 14 February 2012

Encapsulation:


Encapsulation is a process of hiding irrelevant data and showing relevant data to user.

In this, we can show only essential features of a class to user and background details of a class are kept hidden from user.

The user can perform limited operations on hidden data of the class by methods.

Access modifiers are used to achieve the encapsulation in object oriented programming language.

Access modifiers:

In c#.net, we have four access modifiers

· Public

· Private

· Protected

· Internal

By using this we have five accessibility levels they are

Ø Public

Ø Private

Ø Protected

Ø Internal

Ø Protected internal

Public:

it gives unrestricted access to members inside and outside a class.

Example:

Class A

{

Public int i;

}

Private:

It gives access to the members of inside the class only. If we declare private to any member, it will not access outside of the class. By default, class members are private.

Example:

Class A

{

Private int i;

Float x;//by default, it is private.

}

Protected:

It gives access to members of the class and its derived class members.

Example:

Class A

{

Protected int i;

}

Class B: A

{

Int j;

j=i;

}

Internal:

It gives access to the members that are present in the current assembly. If a member with internal access modifier is accessed from outside the assembly in which it has been defined, an error is generated.

Example:

Public class A

{

Internal int x=0;//accessible within the same assembly

}

Protected internal:

It gives access to the members that are visible either to the current assembly or to the types derived from the class in which they are declared.

No comments:

Post a Comment