Topic : Encapsulation in C# Programming Language.

Encapsulation:

Encapsulation is a Programming Mechanism that binds together code and the data it manipulates, and that keeps both safe from outside interference and misuse. In an object-oriented language, code and data can be bound together in such a way that a self-contained black box is created. Within the box are all Necessary data and code. When code and data are linked together in this fashion, an object is created. In others words, an object is a device that supports encapsulation.



image: Data-Encapsulation

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.


Access SpecifierDefinition:
Public:The class Member that is defined as Public can be accessed by other class Member that is initialized outside the class. A public member can be accessed from anywhere even outside the namespace.
Private:The Class member that is defined as Private, can't be accessed from outside the class. The Private access specifiers restrict the member variable or method to be called outside from the parent class.
Protected:The Protected access specifier hides its member methods and variables from other classes. This type of variable or methods can only be accessed in the child class. if we want to inherit some properties of a method from one class to another, then we declare our method or variable as Protected:
Internal:The Class member that is defined as internal, can be access within the Program that contain its declarations. we can access any internal declared member within the same namespace that contains its declaration. but its hide from other classes that resides in another namespace. it is a default, its mean if we didn't mention any access specifier, then "internal" will kick in:
Protected Internal:The protected internal access specifier allows its members to be accessed in derived class, containing class or classes within same application. However, this access specifier rarely used in C# programming but it becomes important while implementing inheritance.


Why we Need Encapsulation?

The Need of Encapsulation is to protect or Prevent the Code/data from accidental Corruption due to some little errors that we are all prone to make. In OOP data is treated as critical element in the Program development and data is packed closely to the functions that operate on it and protects it from accidental modification from outside functions.



Opening Members to Public Access:

We can Open any members to Public using the Public Access Specifier. Public Access Specifier Allows a Class to Expose its member variables and member Methods to other Methods and Object. Any Public member can be Accessed From Outside the Class. Let's have an Example, to Know how this public access specifier work, how to use it.



/*Example: Public Access Specifier: InfoBrother*/

using System;

namespace Access_Specifier
{
    class @public      //First Class: 
    {
        public int x = 10;   //Public Properties:
        public int y = 20;

        public int add()   //Public Method:
        {
            return x + y;
        }
    }


    class Program  //Another Class: 
    {
        static void Main(string[] args)
        {
            @public obj = new @public();  //Object:
            Console.WriteLine("{0} + {1} = {2}", obj.x, obj.y, obj.add());

            Console.ReadKey();
        }
    }
}


In Above example, Did you Notice that, We declare some properties and one method as Public. And we can access it from the outside of that class. we declare these Public properties and method in one class and call it in another class.



Hiding Members with Private Access:

We can set our members as Private using Private Access Specifier. Private Access specifier Allows a class to hide its Members Properties and Methods from other Methods and Objects. When we declare our Members as Private, Only method of the same class can access its Private Members. Even an Instance of the same class can't access it Private Members. Let's have an example here to know how this Access Specifier work.



/*Example: private Access Specifier: InfoBrother*/

using System;

namespace Access_Specifier
{
    class @private      //First Class: 
    {
        private int x = 10;   //Private Properties:
        private int y = 20;


        public int add()     //Public Method:
        {
            return x + y;
        }
    }


    class Program  //Another Class: 
    {
        static void Main(string[] args)
        {
            @private obj = new @private();  //Object:
            Console.WriteLine("The result of x and y is: " + obj.add() );

            Console.ReadKey();
        }
    }
}


In Above Example, We create some Private Properties, and a single Public method add(). This method can access the private Properties of its own class. but outside the class, no method can access the private members, even the Object of same class can't access the private members. if we try to access, the compiler won't let us do it.



Access for Derived Types with the Protected Access Specifier.

In Some ways, the protected access Modifier acts like both the Private and Public access Modifiers. Like private, it only allows access to members within the same type, except that it acts like public only to derived types. In another way, the Protected type members can only be accessed by either member within the same type or members of derived types.

We will read more about Protected Members in our Inheritance Tutorial.


Internal Access Specifiers

Internal Access Specifier allows a class to expose its member properties and methods to Other methods 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.



Protected Internal Access Specifier:

The Protected Internal Access Specifier allows a Class to hide its member variables and Member Methods from the other class objects and methods, except a child class within the same application.

We will read more about Protected Members in our Inheritance Tutorial.


  • More About Encapsulation:

    » Through Encapsulation, a Class can hide the Internal Details of how an Object Does Something.
    » Encapsulation Solves the Problem at the Implementation level.
    » With the Help of Encapsulation, a Class can change the Internal implementation without hurting the Overall functionality of the System.
    » Encapsulation Protects Abstraction.
    » Encapsulation help to manage the security of Members with the help of Access Modifiers.















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement