How Encapsulation is different from data hiding

I am new here I am confused by reading this page https://en.wikipedia.org/wiki/Data_encapsulation

This page says Data encapsulation, also known as data hiding

and other links says Data Hiding are two different things.

Encapsulation means to protect sensitive information of an object by making members protected or private in a class

Data hiding means to make sure that sensitive data is hidden from user

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  class Employee {
private:
// Private member
int salary;

public:
// Setter
void setSalary(int s) {
      salary = s;
    }
// Getter
int getSalary() {
return salary;
    }
};


In my code what is data hiding and what is encapsulation ?
The class encapsulates everything about an Employee and the Employee's salary data is hidden.

salary is private and is only accessible by the get and set methods
encap means something is hardened against accidental or incidental modification and wrapped up in a protective layer (class/struct in c++, or an 'object' usually. There are some other things that have these concepts like a namespace or file scoped static but for now consider them to be class members). A public class variable is not considered (fully) encapsulated. A protected or private variable with getters and setters is encapsulated: it can only be modified or read via controlled interfaces.

a global variable is the opposite of this idea -- anyone, anywhere can modify it without warning to the other things that use it.

Topic archived. No new replies allowed.