Net-informations.com
SiteMap  | About    

What is Data Encapsulation?

Data Encapsulation in C# vb.net asp.net

Data Encapsulation is an Object Oriented Programming concept that bind a group of related properties, functions, and other members are treated as a single unit. Class is the best example of Data Encapsulation. It sometimes referred to as data hiding that prevents the user to access the implementation details. Encapsulation therefore guarantees the integrity of the data contained in the Object.

What is encapsulation?

The whole idea behind the data encapsulation is to hide the implementation details from users. This is achieved through the state (the private fields) and the behaviors (the public methods) of a Class.

public class Student { private string studentName; //Retrieve name public string Getname() { return studentName; } // Set name public void Setname(string inName) { studentName = inName; } } pub;ic static void Main() { string sName ="Jack"; Student obj = new Student(); obj.Setname(sName); System.Console.WriteLine("Name :" + obj.Getname()); }

Object oriented programming data encapsulation

In the above example we use the Getname() and Setname() methods to retrieve and set student name. We use the private variable studentName and as it is not accessible directly by the user. So, in order to access this variable, we use the Getname() and Setname() methods respectively. This means that a user does not need to know how implementation takes place. Here we can see the data encapsulation is achieved through the state (the private fields) and the behaviors (the public methods) of an Object. This way only the data can accessed by public methods, by making the private fields and their implementation hidden for outside classes. That's why data encapsulation is known as data hiding.

Data Encapsulation is implemented by using access specifiers (Access Modifiers) and it defines the scope and visibility of a class member. In C# , Encapsulation uses five types of modifier to encapsulate data.

Public Private Protected Internal Protected internal

You can see here more details about... . Access Specifiers







net-informations.com (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.