+ Start a Discussion
ashish jadhav 9ashish jadhav 9 

what is encapsulation in salesforce? Any example?

can someone please let me know how we can do encapsulation in salesforce or how that concept is implemented in it?
nagendra 6989nagendra 6989
Hi ashish jadhav 9,

As you want to understand the concept of encapsulation and its implementation in salesforce please find the below explanation.

Encapsulation : It is an Object Oriented Programming concept, which binds the programming elements together in a single unit that manipulate the data, and that keeps both safe from outside interference and misuse.

 It is all about hiding the code to secure the contacts of a program. To hide the code we enclose the members of a program under a container or wrapper as class.

Programming Elements : Data and Instructions together can be called as programming elements.

In other words data encapsulation has led to one of the important concept of information hiding mechanism.

Binding of Programming Elements :  It’s simply stating that a piece of code should be grouped together with the data and actions important to that code’s purpose. 

Why to use Encapsulation : 

a)Provides namespaces/unique names for application data and functionality making code more manageable e.g. in the old days when classes didn’t exist and all your code would be lumped together you could only have one function called calculate(). With classes you can group functionality into classes and not have to think up crazy and creative names for your variables and methods e.g. Tax.calculate() and IQ.calculate().
b)Protecting data that is read-only or irrelevant to the consumer.
c)Control over how variables are managed e.g. validating input before assigning it to a variable.
d)Code is easier to read, maintain and extend.
e)OOP is impossible without it!

 Example : 

 class and interfaces.

Class classname {
//Binding elements in class

Encapsulation can be done with different apex Classes, Methods, Access Modifiers, Properties, Variables.

For more information please go through the below link .


Please let me know if it helps you ................

Thanks & Regards