+ 1

Differnce between encapsulation and data abstraction in phyton

24th Feb 2019, 4:39 PM
Sahil Javed Khan
Sahil Javed Khan - avatar
10 Réponses
+ 2
In my head, abstraction is the consequence of encapsulation. You take a complicated situation; you hide everything messy about it and only leave a small window (let's say a getter method) to access the result, and as a consequence you have an abstraction that behaves simple although internally it's complex. Hm, shall we take Python's int as an example? On the surface it looks like an int in any other language; but if you take a closer look, there's stuff happening in the background. For example overflow doesn't happen, ints just grow and grow, although 'raw' ints like in C would overflow after 2,147,483,647. But what's happening with the raw data, is encapsulated away from our eyes, and what remains is 'just int' - only that it's an abstraction of what's normally considered an int.
24th Feb 2019, 5:20 PM
HonFu
HonFu - avatar
+ 1
So when you say abstraction comes first, you are talking about the designing that the coder does before implementing, like: 'What do I even want? Well I want an int that doesn't overflow. So what do I need to do to get it, what do I need to hide, how is the interface supposed to look like?' Then design-wise the abstraction would come first. But abstraction is also the word for the state of the thing that has been abstracted, right? Like our image of a 'radio' is an abstraction from all the internal electronic stuff we don't need to look at. From that point of view, you'd hide away stuff and by doing that you'd for the first time create the abstraction. Maybe it is not so helpful to define what comes first? Is it rather interrelated? But it may be an interview question, so probably no way around it. ;-)
24th Feb 2019, 6:44 PM
HonFu
HonFu - avatar
+ 1
So in short: abstraction == interface?
24th Feb 2019, 7:30 PM
HonFu
HonFu - avatar
+ 1
I have to learn many terms *just back from googling contracts* 😅
24th Feb 2019, 7:44 PM
HonFu
HonFu - avatar
0
~ swim ~, can you find a simple, beginner-friendly example to illustrate how abstraction preceeds encapsulation?
24th Feb 2019, 6:16 PM
HonFu
HonFu - avatar