Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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();
        }
    }

 
        

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post