Home > DeveloperSection > Blogs > Encapsulation

Encapsulation


.NET .NET 
Ratings:
0 Comment(s)
 1727  View(s)
Rate this:

Encapsulation

Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required. Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods. We can implement Encapsulation using Assessors.

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.

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.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Encapsulation

{

    class EncapTest

    {

        private String custId; //private members

        private String name;  //private members

        private int age;     //private members

        private int openingBalance;  //private members

        public String getCustID()  //Creating Public methods to manipulating private data

        {

            return custId;

        }

        public int getCustAge()

        {

            return age;

        }

        public String getCustName()

        {

            return name;

        }

        public int getCustOpeBal()

        {

            return openingBalance;

        }

        public void setCustId(String newId)

        {

            custId = newId;

        }

        public void setCustAge(int newAge)

        {

            age = newAge;

        }

        public void setCustName(String newName)

        {

            name = newName;

        }

        public void setOpeBal(int newBal)

        {

            openingBalance = newBal;

        }

    }

    class Program

    {      

        static void Main(string[] args)

        {

            EncapTest encap = new EncapTest();

            encap.setCustName("Ramesh"); //setting the value in private member through public methods

            encap.setCustAge(20);

            encap.setCustId("123");

            encap.setOpeBal(5000);

            Console.WriteLine("ID : " + encap.getCustID() + "\n" + "Name : " + encap.getCustName() + "\n" + "Age : " + encap.getCustAge() + "\n" + "Opening Balance : " + encap.getCustOpeBal());

        }

    }

}

The public methods are the access points to this class's fields from the outside class. Normally these methods are referred as getters and setters. Therefore any class that wants to access the variables should access them through these getters and setters.

Output:-

ID : 123

Name : Ramesh

Age : 20

Opening Balance : 5000

 


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

Follow MindStick