• Have Any Question?
  • contact@eitworld.com

csharp encapsulation





C# Encapsulation, Abstraction and Access-Specifier

 

Encapsulation

 

  • Encapsulation is simple concept which prevents the access to data that is not essential.
  • It is a process of binding the variable and methods together required by an object in a single unit that is class.
  • Encapsulation is a technique of hiding internal details of a class to prevent data to be accessed from the outside world.

Abstraction

 

  • Abstraction is the process of providing the essential features of an object without providing its background details or explanation.
  • Classes use the concept of abstraction that is why classes are known as abstract data type.

Access Specifiers

 

    • Access specifier defined the scope of the members of class.
    • In C# features of encapsulation is implemented through access specifiers, by restricting the flow of data across method and classes.
    • All access specifiers are keywords.
    • Here is a table that shows the various access specifier with their description:

access-specifier

Let us understand each of the access specifier with the help of an example:

 

public access-specifier

 

Public members are accessible in the class in which they defined and also accessible from anywhere outside the class, including classes derived from its base class.

 

Here is an example of public access-specifier:

 

Example

 

using System;
namespace accessspecifier
{
class publicexample
{
public int a;//public variable declaration
public void print()//public method declaration
{
Console.WriteLine(“C# is interesting”);
}
}
class public1
{
static void Main()
{
publicexample a1 = new publicexample();
// Accessing members f public access specifier that is outside the class
a1.a = 6;
a1.print();
Console.ReadLine();
}
}
}

The output of the above program is as follows:

 

Output

 

C# is interesting

private access specifier

 

    • The members of private access specifer are accessible only inside their own class.
    • A private method or variable cannot be called outside from the same class. It hides its member variable and method from other class and methods.
    • ‘Properties’ are used to access the private members.
    • Here is an example is given for the private access specifier:
Example

 

using System;
namespace accessspecifier
{
class privateexample
{
private int a;//private variable declaration
private void print()//private method declaration
{
Console.WriteLine(“C# is interesting”);
}
}
class private1
{
static void Main()
{
privateexample a1 = new publicexample();
// Accessing members of private access specifier that is outside the class
a1.a = 6;//raies an error
a1.print();//raise an error
Console.ReadLine();
}
}
}

The error will be generated on executing the above program because we can’t access private members outside the class so the output is:

 

Output

 

1.’accessspecifier.privateexample.a’ is inaccessible due to its protection level.
2.’accessspecifier.privatexample.print()’is inaccessible due to its protection level.

protected access-specifier

 

    • Protected members are accessible in its own class and class derived from its base class. It plays a crucial role in the concept of inheritance.
    • Here is an example of the protected access-specifier.
Example

 

using System;
namespace accessspecifier
{
class protectedexample
{
protected int a;//protected variable declaration
protected void print()//protected method declaration
{
Console.WriteLine(“C# is interesting”);
} }
class public1
{
static void Main()
{
protectedexample a1 = new protectedexample();
// Accessing members of protected access specifier that is outside the class
a1.a = 6;//raies an error
a1.print();//raise an error
Console.ReadLine();
}
}
}

The error will be generated on executing the above program; this is because the protected member can only be accessed within its child class or its own class so the output is:

 

Output

 

1.’accessspecifier.protectedexample.a’ is inaccessible due to its protection level.
2.’accessspecifier.protectedexample.print()’is inaccessible due to its protection level

internal access specifier

 

    • internal members are accessible classes of the same assembly.
    • The variable or classes that are declared with internal can be access by any member within application.
    • The default access specifier of class in C# is also internal.
    • Here is an example of internal access specifier:
Example

 

using System;
namespace accessspecifier
{
class protectedexample
{
internal int a;//internal variable declaration
internal void print()//internal method declaration
{
Console.WriteLine(“C# is interesting”);
}
}
class public1
{
static void Main()
{
protectedexample a1 = new protectedexample();
// Accessing members of internal access specifier that is outside the class
a1.a = 6;
a1.print();
Console.ReadLine();
}
}
}

The output of the above program will be as follows:

 

Output

 

C# is interesting

protected internal access-specifier

 

protected internal members are accessible in the derived classes, its own class and also in the classes of its same assembly. Here is an example of protected internal access specifier:

 

Example

 

using System;
namespace accessspecifier
{
class protectedexample
{
protected internal int a;//protected internal variable declaration
protected internal void print()//protected internal method declaration
{
Console.WriteLine(“C# is interesting”);
}
}
class public1
{
static void Main()
{
protectedexample a1 = new protectedexample();
// Accessing members of protected internal access specifier that is outside the class
a1.a = 6;
a1.print();
Console.ReadLine();
}
}
}

The output of the above program will be as follows:

 

Output

 

C# is interesting



Real Time Web Analytics