Home > DeveloperSection > Articles > Encapsulation in C#.Net

Encapsulation in C#.Net


C# C# 
Ratings:
1 Comment(s)
 4754  View(s)
Rate this:

Encapsulation in C#.Net

Encapsulation is a method for protecting data from unwanted access or alteration by packaging it in an object where it is only accessible through the object's interface. Encapsulation is often referred to as information hiding but both are different. In fact information hiding is actually the result of Encapsulation. Encapsulation makes it possible to separate an object's implementation from its original behavior - to restrict access of its internal data. This restriction facilitates certain details of an object’s behavior to be hidden. This allows protecting an object's internal state from corruption by its user.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etc. It means this is the property of encapsulating members and functions.

    class Bag

    {

       string book;

        int pen;

        void ReadBook();

    }

Encapsulation means hiding the internal details of an object, i.e. how an object does something.

Encapsulation prevents clients from seeing it’s inside view, where the behaviour of the abstraction is implemented.

Encapsulation is a technique used to protect the information in an object from the other object.

Hide the data for security such as making the variables as private, and expose the property to access the private data which would be public.

So, when you access the property you can validate the data and set it.

Real Example: when we fill withdrawal form of bank, we don’t know what process will be going on. We only take money and go outside the bank. Means process of money withdrawal is called encapsulation. 

public class User

    {

        public void Info()

        {

            Console.WriteLine("Account Holder Information…");

        }

 

     }

 

    public class Account : User

    {

        public void Check()

        {

            Console.WriteLine("User Verify…");

        }

 

        public void withdrawal()

        {

            Console.WriteLine("Collect Money...");

        }

    }

 

    public class Bank : Account

    {

        public static void Main()

        {

            Account ac = new Account();

            ac.Info();

            ac.Check();

            ac.withdrawal();

            Console.ReadLine();

        }

    }

 

 

 

In above example, user filled information and just wait for money. But user doesn’t have idea about account checking process and withdrawal process.  


About article

By Nwe Nwe on   3 years ago
Thanks a lot for explanation in detail.

Don't want to miss updates? Please click the below button!

Follow MindStick