Data Encapsulation Example in C#

0
Data Encapsulation Example in C#:- 

It means binding of data using a single unit, Class provides essential details and non-essential details. show essential details and hide non-essential details is managed by abstraction and associate all information under method block is managed by encapsulation.

We should always create private data members because they should not be accessed directly, it must be defined under the method block to access them.


Encapsulation means packing of data in a single unit, it is the complement to data abstraction because without encapsulation we can not implement the data abstraction process in the program.


Now I am explaining the example of Data Encapsulation in C# with a practical example.

class Classname
{
      private int a,b,c; 
      void MethodName()
     {
            a=100;
            b=200;
            Functionality;
     }

}

Now we create the Object:-

Classname ref = new Classname();
ref.MethodName();

Example of Data Encapsulation Completely:-

class EncapsulationExample
    {
        private int a, b, c;
        private void DisplayHello()
        {
            a = 100;
            b = 200;
            Console.WriteLine(a + b);
        }
        internal void DisplayHello1()
        {
            DisplayHello();
        }
    }
    class EncapMain
    {
        public static void Main()
        {
            EncapsulationExample obj = new EncapsulationExample();
            obj.DisplayHello1();
            Console.ReadKey();
        }
    }

Now I am creating a class to enter the password where the password data member is private and 

member function is internal, this is also the best example of data abstraction.

 class Password
    {
        private String password;
        internal void EnterPassword()
        {
            Console.WriteLine("Enter password");
            password = Console.ReadLine();
            Console.WriteLine("Password is " + password);
            Console.ReadKey();
        }
    }



Now I am creating another example of a Bank class that will hide all transaction methods and show only login methods, all methods will be encapsulated under login.


class Bank
    {
        private int balance = 5000;
        private void Credit(int amt)
        {
            balance += amt;
        }
        private void Debit(int amt)
        {
            balance -= amt;
        }
        private void CheckBalance()
        {
            Console.WriteLine("Balance is " + balance);
        }

        internal void Login(int pincode)
        {
            if (pincode == 1223)
            {
                this.Credit(1000);  //Encpaulated under Login
                this.CheckBalance(); // Encapsulated By Login
            }

        }
    }



Create Another class Customer:-

class Customer
    {
        static void Main()
        {
            Bank obj = new Bank();
            obj.Login(1223);
            Console.ReadKey();
        }
    }

 
        
Tags

Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)