Saturday, 7 September 2013

Abstraction and Encapsulation in C#

What is Encapsulation ?
      Encapsulation is a one of the principle of object oriented programming. The concept of encapsulation is hiding  the Data from the outside world, i.e Hiding the Member, Field, Property from the outside of the class is known as Encapsulation.

     In Real time Example how we can understand that. Let we take Laptop as Example. In Laptop we know how to operate , When pressing the windows key , Start up is launched, But how it is launched is not exposed to the user, here it is encapsulated. Make this process as private from user.

  In C# encapsulation is done by Access Specifier "private" keyword

Example 


    public class Car
    {
        public string Model { set; get; }

        private int EngineSpeed;

        public void Start()
        {
       
        }

        public void Off()
        {
       
        }

        private void EngineProcess()
        {
            EngineSpeed = 170;
        }
    }

    public class Driver
    {
        public void work()
        {
            /* Car class hides the EngineProcess() member and EngineSpeed field from outside the class (Encapsulation)*/
            Car Honda = new Car();
            Honda.Model = "SDF324";
            Honda.Start();
            Honda.Off();
        }
    }



   In the above example , Car class encapsulates the EngineProcess member from outside the class and also EngineSpeed Field is hides from outside the class.

What is Abstraction ?
     Abstraction is also another importance concepts of Object Oriented Programming. Abstraction is Process of Exposing a necessary information and Data to the outside of class. using the access specifiers.

In C# we have different types of access specifier,
1. private
2. public 
3. protected
4. internal
5. protected internal

What is the usage of access specifier ?

Access specifier is used to specify the what type of access can given for method,class,variable,property, event when it is try to invoke from other class , other class which is present in other project. In C# there is various Access Specifiers
1. Public
2. Private
3. Protected
4. Internal
5. Protected Internal

Here Assembly consider as Project in IDE

public

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

private
The type or member can be accessed only by code in the same class or struct.

protected
The type or member can be accessed only by code in the same class or struct, or in a class that is derived from that class.

internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.

protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.


2 comments:

  1. This is an amazing blog,it gives very helpful messages to us.Besides that Wisen has established as Best Dot Net Training in Chennai. or learn thru ASP.NET Online Training . Nowadays Dot Net has tons of job opportunities on various vertical industry.

    ReplyDelete