articles

Home / DeveloperSection / Articles / What is Encapsulation in c#

What is Encapsulation in c#

Anonymous User 7582 06-Jun-2012

Encapsulation is the procedure of covering up of data and functions into a single unit (called class). Encapsulation hides the internal state and behavior of an object. Encapsulation is a process of hiding all the internal details of an object from the outside world. Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required.

Type Member Access Modifiers:

We use access modifiers such as private, public, protected. It provides a way to protect data. An access modifier allows you to specify the visibility.

private

Only within the same class. (default for class members)

protected

Only in derived class or in the same class.

internal

Only use within the same assembly. (default for types)

protected internal

Either use in derived assembly or use in the same assembly.

public

Any where use.

Example of encapsulation:

For example, we create a CustomerDetails class that has private, protected and public variables and functions. Also create class Update Balance that use protested member of base class Customer details. There is another class created called CustomerType inherits the UpdateBalance use the public member of CustomerDetails class.

What is Encapsulation in c#

Write down the following line of code.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace abstraction

{

    public partial class frmEncapsulation : Form

    {

        public frmEncapsulation()

        {

            InitializeComponent();

        }

 

        public class CustomerDetails

        {

            private int CustomerID; //private type variables accessable only same class

            private string CustomerName; //private type variable accessable only same class

            protected double Balance;   //protected type variable accessable only same class and 1 level derived class

            public string CustomerType; //public type variable accessable anywhere

 

            private void Input(int customerId, string customerName) //private method not directly called

            {

                CustomerID=customerId;  //use the private member in same class

                CustomerName=customerName;  //ues the private member in same class

            }

 

            public void Input(int customerId, string customerName, double balance)//public function

            {

                Input(customerId, customerName); //public function call the private method in the the same class

                Balance=balance;//use of protected function in the same class

            }

            public void Display()   //public function

            {

                MessageBox.Show("Customer Details\nCustomer ID..." + CustomerID+"\nCustomer Name..."+CustomerName+"\nCustomer Balance..."+Balance);

            }

        }

        public class BalanceUpdate : CustomerDetails  //creatin child class BalanceUpdate from the base class CustomerDetails

        {    //so BalanceUpdate automatically derived the protected, public property/method of CustomerDetails

            public void AddBalance(double balance)//create the public function

            {

                if (balance > 0)

                    Balance = Balance + balance; //access the prtected member of base class

                else

                    MessageBox.Show("give valid balance");

            }

            public void SubstractBalance(double balance) //create the public function

            {

                if (balance > 0)

                    Balance = Balance - balance; //access the prtected member of base class 

                else

                    MessageBox.Show("give valid balance");

            }

           

        }

        public class CustomerType : BalanceUpdate //creatin child class CustomerType from the base class BalanceUpdate

        {   /BalanceUpdate also a child class of a CustomerDetails so CustomerType automatically derived the only public property and method of CustomerDetails

            public void GetType(string customerType)

            {

                CustomerType = customerType; //access the public member of CustomerDetails class

            }

            public void Show()

            {

          Display(); //access the public function of CustomerDetails class

   MessageBox.Show("Customer Type Details\nCustomer Type..." + CustomerType);

            }

        } 

 

        private void buttonShow_Click(object sender, EventArgs e)

        {

            CustomerType CT = new CustomerType(); //creating object of CustomerType class

            double Bal = Int32.Parse(txtBalance.Text);

            CT.Input(Int32.Parse(txtCustomerID.Text), txtCustomerName.Text, Bal);//access the public function of CustomerDetails class

            CT.GetType(txtCustometType.Text);//access the public member of CustomerType class

            CT.Show();      //access the public member of CustomerType class

 

            CT.AddBalance(3000);//access the public member of BalanceUpdate class

            CT.Show();//access the public member of CustomerType class

 

            CT.SubstractBalance(2000);//access the public member of BalanceUpdate class

            CT.Show();//access the public member of CustomerType class

 

        }

    }

}

 


 Fill the required fields and click on the button ‘Show’.

What is Encapsulation in c#

 

What is Encapsulation in c#

Display the information of Customer details.



Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By