0

I have been trying to understand the concept of encapsulation and abstraction for quite sometime through many links and youtube videos but still have some confusion related to the concept of encapsulation.

According to the understanding I have related to the concept of Abstraction and Encapsulation is :

Abstraction is showing only what is necessary

Encapsulation says hide complexity

For Eg :

If we have customer application to add a customer and his related details like address by getting a database connection.

In this scenario what I understood about abstraction is showing only addCustomer() method in our class and rest of the operation that we perform when add method is called like getDBConnection() and addCustomerAddress() is hidden inside it.

**

public void addCustomerDetails(){
    getDBConnection();
    addCustomer();
    addCustomerAddress();
}

**

So here whoever is using our class just need to call addCustomerDetails()(i.e.Abstraction) and need not be worried about how db connection is made and how those operations are performed i.e.(Hiding details or Encapsulation)

So far everything is good but somewhere I read another definition related to encapsulation as in :

Data Encapsulation is the process of combining data and functions into a single unit called class.

It can be achieved through keeping data members as private and functions public so that nobody can access it directly.

Kindly tell me what is the correct definition of Encapsulation and how?

Thanks.

user2412380
  • 21
  • 3
  • 10
  • Thanks, but I have gone through the link before which you have provided but still had confusion so posted my question – user2412380 Jan 30 '16 at 09:49
  • 1
    You will find that definitions are a bit fuzzy and people use words differently. You have a solid grasp on what the concepts, at this point you should probably proceed with coding and accept this lack of complete clarity. It will happen again and again. – jackmott Jan 30 '16 at 10:35
  • Replace "many links and youtube videos" with "many **textbooks** and years of **practical experience**". – Christian Hackl Jan 30 '16 at 11:28

2 Answers2

0

The idea of encapsulation can be initially thought as protecting your class' fields.

What I usually think about Encapsulation is that you don't provide public access to entities outside your class (that is external classes) and if you wish so, you do that using public methods (usually getters and setters).

So let's take an example:

private String field1;

This field can be only accessed from methods within this class and nothing else; neither read nor write access for external classes.

If you wish to give read access, you implement a getter for that field:

public String GetField1() { return this.field1 }

Now, here is the good practice with encapsulation, if you want now to give write access you will do the following:

public void SetField1(string value)
{
   if(some_logic_here}
   {
      // If and only if true, then set
      this.field1 = value
   }
}

So your class' attributes won't get random values, but will pass through some checks, to ensure that the data are valid and appropriate for what the field represent.

So to summarise about Encapsulation:

  • Does expose only the necessary parts of the class.
  • Does hide the unnecessary parts of the class from external entities.
Rafael
  • 7,002
  • 5
  • 43
  • 52
0

Think of encapsulation this way, then it can actually connects your 1st definition of encapsulation to your 2nd definition.

Encapsulation = "combining data and functions into a single unit called class" (your 2nd definition)

Each private data member in a class cannot be accessed from outside of the class. So, it's effectively hidden. Only methods/functions that are within the class can access them. From outside the class, in order to manipulate the private data member in the class, you can only do so by calling public method/functions that are defined within the same class.