Hi,
Ques Can you explain encapsulation and abstraction in the .Net?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
chirag pandyaPosted Mar 15, 2012, 3:27 AM
Please see below live example for both concepts
Suppose goverment declare tax policy of 2012. Goverment provide a webservice and create a webmethod that CalulateTaxFor2012(decimal income,decimal expense). So any firm want to know about it's Tax then they will utilize this webservice Interface(CalulateTaxfor2012 by passing parameter).
Abstraction:- Goverment hide the tax calulation. any firm owner know only Interface name CalulateTaxFor2012 and its parameter(income,expense) but doesn't know how goverment calulated this tax.
Encapsulation:-Goverment shows client only limited things like CalculeTaxFor2012 method and it's parameter other important things they wrap in there business logic.
Jignesh TrivediPosted Mar 14, 2012, 11:40 PM
Encapsulation is one of the fundamental principles of object-oriented programming.
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
In this example _a is private can not access out side world means datahinding.
class ABC
{
private string _a;
public string A
{
get{ return _a}
set{ _a = A}
}
}
Abstraction means to show only the necessary details to the client of the object.
for example if we have third party dll then they shows ony nessary infomation like methos name, paramerter, type, return type of methods, donot expose internal logic variable.
Abstraction solves the problem in the design level where as Encapsulation solves the problem in the implementation level
Abstraction is used for hiding the unwanted data and giving relevant data, but Encapsulation hiding the code and data in to a single unit to protect the data from outside world.
hope this help.
Satyapriya NayakPosted Mar 14, 2012, 11:32 PM
Encapsulation is the ability to hide the internal workings of an object's behavior and its data. For instance, let's say you have a object named Bike and this object has a method named start(). When you create an instance of a Bike object and call its start() method you are not worried about what happens to accomplish this, you just want to make sure the state of the bike is changed to 'running' afterwards. This kind of behavior hiding is encapsulation.