Hello friend,
What are the basic differences between Encapsulation and Abstraction?
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.
Sujeet SumanPosted Oct 20, 2015, 2:46 AM
AartiPosted Nov 16, 2011, 12:27 AM
Encapuslation can be seen as per developer prospective. Developer does'nt want that any one can change his code so he tries to hide the data using private access modifier and providing getter and setter methods. But Abstraction belongs to the person who is using that code. He is not able to see the implementation of code but he uses the certain API provided by developer to use developer's code.
Datta KharadPosted Nov 15, 2011, 8:58 AM
Abstract classes provide a way to force an inherited class to implement an override method, similar to, but not the same as an interface. Encapsulation is the ability for an object to hide its data and methods from those who do not need to know, and only expose data and methods that are required.
1) Abstraction:-
Abstract classes are fully functional classes, but you can force one or more methods to be overridden.
Lets say you had a theoretical vehicle class. Every vehicle must be able to steer (i.e. must have a steer() method) however different types of vehicle will have different implementation of this method. It is pointless to implement this method in the base class as the code will not be used and could not possibly contain enough information for the derived class to use. It is also dangerous to leave it empty as it could be called without any implementation causing the resulting vehicle to be unable to steer! By marking the method as abstract, you force any derived classes to have their own specialised implementation of the method.
Both the class and the method must be marked as abstract. Because there is no implementation of the steer method, the statement is terminated with a semi-colon.
public abstract class Vehicle
{
public abstract void steer();
}
Now, each class that inherits from the Vehicle class must implement the steer method.
public class Car : Vehicle
{
public override void steer()
{
// Implement a steering wheel
}
}
public class Bike : Vehicle
{
public override void steer()
{
// Implement handlebars
}
}
public class Tank : Vehicle
{
public override void steer()
{
// Implement tracked drive steering
}
}
public class Airplane : Vehicle
{
public override void steer()
{
// Implement ailerons and rudders
}
}
2) Encapsulation:-
Encapsulation is the ability for an object to hide its data and methods from those who do not need to know, and only expose data and methods that are required.
Techniques for encapsulation include class abstraction and properties. Sometimes you may not wish direct interaction with an objects data fields, for example in a bank account class you would not want to expose a balance field where it could possibly be modified without any checks or security.
Encapsulation is the process of protecting the data using properties, so that any attempt to modify the data is done through the proper channels, ideally with an audit log for the bank account class.
Using the rather cliché BankAccount class as an example, we can encapsulate the account balance to prevent invalid or unwanted access. Yes, I know that this is not how a bank account should function, but it serves for the purpose of this example.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class BankAccount1
{
public decimal Balance;
}
class BankAccount2
{
private decimal _balance;
public decimal Balance
{
get
{
return _balance;
}
set
{
// do security checks and validation
if (value > 0)
_balance = value;
else
throw new InvalidOperationException();
}
}
}
class Program
{
static void Main()
{
BankAccount1 ba1 = new BankAccount1();
ba1.Balance = -100; // Dangerous as this is allowed
BankAccount2 ba2 = new BankAccount2();
ba2.Balance = -100; // This is now not allowed and raises an error
}
}
}
Please mark as Accepted answer if your query resolved.
AartiPosted Nov 15, 2011, 8:10 AM
Encapsulation mean hiding data behind methods. genarally private members can be accessed through public methods only.
Abstraction means not exposing the implementation details .
Thanks.
VulpesPosted Oct 11, 2011, 2:07 PM