3

My teacher told me that encapsulation is data/information hiding.

But what I understand from here is that Encapsulation is bundling data and methods that act on that data into one unit. And that [encapsulation] allows us to create information hiding mechanisms. Like, making a variable read-only, or making it accessible through some checkpoints.

Am I right that encapsulation in itself is not data hiding, but a way through which we can hide data?

Jack
  • 1,453
  • 1
  • 15
  • 35
Tayyab Mazhar
  • 1,560
  • 10
  • 26
  • yea, hiding data is only part of what it could do. not the main purpose – Steve Nov 02 '18 at 14:30
  • 3
    The body hides the details of its nutritional uptake system. You don't feed yourself by injecting your organs with nutrients. You have a single pathway for nutritional intake (your mouth). The details of what happens after that are pretty well hidden. A well designed class has an external interface that makes sense to consumers (say, a mouth and a mechanism for excreting waste) and a perhaps complex set of data an operations behind that interface (esophagus, stomach, intestines, etc.). – Flydog57 Nov 02 '18 at 14:34
  • I'm lazy today:https://www.techopedia.com/definition/3787/encapsulation-c – Trey Nov 02 '18 at 14:35
  • Possible duplicate of [Encapsulation vs Data Hiding - Java](https://stackoverflow.com/questions/12013448/encapsulation-vs-data-hiding-java) – Jack Nov 02 '18 at 14:41
  • Encapsulation, in itself, doesn't necessarily imply data hiding. I think of encapsulation as different levels of software or data structures, each level acting mostly as a [black box](https://en.wikipedia.org/wiki/Black_box) to the adjacent levels. Information is often hidden between levels merely because the information about level A isn't needed at level B and vice versa (e.g. if performing an abstract calculation where you only care about the result but not all of the intermediate steps) but sometimes the information is explicitly hidden (e.g. if dealing with information such as passwords). – ImaginaryHuman072889 Nov 02 '18 at 16:26

4 Answers4

3

There is no authoritative source that can tell you with full confidence. You (we all) have to ask unfortunately every time it comes up what exactly the speaker/writer means.

Most of the time is encapsulation a little bit more than information hiding.

Encapsulation is a bit more abstract, and may refer to not just data, but logic or any knowledge in general.

Data hiding is just that (normally), hiding the data (the instance variables).

How these things get implemented is a source of even more debate! For example some (if not most) people refer to data hiding when instance variables are simply declared private. Even if there is a public getter for that same data! (the linked article seem to support this position)

Again for others (myself included) calling data hidden when there is a public getter for it sounds strange to say the least.

Some people insist that getters are ok (the data hiding applies) if the returned data is immutable, since it can not be changed.

Encapsulation is often used together with logic. For example: I encapsulate how to send emails in this class, etc.


The problem is, everyone uses the same words, so it's nigh impossible to tell what someone really means by either of these things. If you want to know what someone is talking about, always demand an example (or two).

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38
1

I will give an explanation to encapsulation and data hiding as I understood from code complete book

When you create a class/method the primary goal is to reduce complexity of your program. You create a class/method to hide information so that you won’t need to think about it. Sure, you’ll need to think about it when you write the class/method. But after it’s written, you should be able to forget the details and use the class/method without any knowledge of its internal workings.

So each class/method should ask a question "What should I hide in order to reduce complexity?" and hence you start to create the interface that this class/method provides to the outside world (other classes/methods). This consists of creating a good abstraction for the interface to represent and ensuring that the details remain hidden behind the abstraction.

Abstraction helps to manage complexity by providing models that allow you to ignore implementation details. Encapsulation is the enforcer that prevents you from looking at the details even if you want to. If I declare a method as private, I'm forcing that it should be used only inside the class and not from outside and if I declare it as public I'm saying that it is part of the interface this class is providing and it can be used from outside. The two concepts are related because, without encapsulation, abstraction tends to break down.

Mohamed Ibrahim Elsayed
  • 2,734
  • 3
  • 23
  • 43
0

It means you can't mess with the other object's innards unless that object lets you. It is encapsulated, in a capsule you can't get into. An artifact of this is hiding information. If you are closed up, and only you can open things up, then you've pretty much hidden everything inside of yourself. Thus, the hiding is a consequence of encapsulating.

johnny
  • 19,272
  • 52
  • 157
  • 259
-3

Think of a Birthday class that takes in a Birthdate (DateTime) in the constructor. It has properties the following that are filled

   Public Property _ZodiacSign As String = String.Empty
    Public Property _ChineseZodiac As String = String.Empty
    Public Property _ChineseZodiacChar As String = String.Empty
    Public Property _is21AndOver As Boolean
    Public Property _ChineseDate As String
    Public Property _EstimatesConvievedDate As DateTime

You have no idea what the logic is to figure out the zodiac sign or chinesezodiac or are they over 21, it is a black box.

China Syndrome
  • 953
  • 12
  • 24