Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    API(s)
    Sencha-Touch
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> C# >> What is Encapsulation in c#
What is Encapsulation in c#
What is Encapsulation in c#


by Arun Singh on 6/6/2012 5:06:48 PM

Views: 1590       Comments: 0

What is Encapsulation in c#

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.

Report Abuse Form
Reason:    
 

Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by Arun SinghRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2691
Advertisement
MindStick SurveyManager
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.