.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
david stephan
Gaurav Pal

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Encapsulation Doubt in my code

Posted By: Naresh     Posted Date: November 30, 2010    Points:2   Category :ASP.Net
Hi All,

Can u tell me whether i am using Encapsulation concept in below code.Why i should use private for variable and public for property.

Can u give any other example for Encapsulation with code .

private string _UserName;

public string UserName
get{return _UserName;}

Author: praveen             
Posted Date: December 06, 2010     Points: 5   

With help of private data we are maintaining security purpose, Encapsulation means security the data,the above code private variable is called encapsulation concept.

Author: Vinodh             
Posted Date: September 16, 2011     Points: 20   

When you create an object out of that class, then your object encapsultes Username.
private variables cannot be used outside the class, whereas public variables can be used by the objects outside the class.

another example :
class Car
public int seats;
public void inceaseSpeed()
Console.WriteLine("Speed increased");
public void decreseSpeed()
Console.WriteLine("Speed decreased");

class Program
static void Main(string[] args)
Car mycar = new Car();
mycar.seats = 4;


Encapsulation: seats, inceaseSpeed() and decreseSpeed()are encapsulated in car
Abstraction:inceaseSpeed() and decreseSpeed() ,driver need not know how they work

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend