-5

Most of the people says that abstraction is hiding something and showing only functionality to the user. Can anyone explain me what are all the things you are hiding and what are all the things you are showing?? please don't explain with the examples of animal, engine, vehicle.

2 Answers2

1

I think this is a case where a concrete example would help a lot.

HashMap has an internal structure for handling hash collisions, which is an implementation of a singly-linked list. Now, do you know how that internal structure works, what it's called, what its fields are called, etc? More importantly, do you care, so long as the HashMap "just works"?

If the answer to both of those is "no" — which is what it should be for anything other than curiosity/learning purposes — then those details have been hidden from you and exposed via the abstraction of Map's interface.

The result is a class that's easier for you to reason about (because you have less to learn), and easier for the library maintainers to maintain (because they don't need to worry about a change they make breaking your code, so long as they still abide by the interface).

yshavit
  • 42,327
  • 7
  • 87
  • 124
  • Your example is off. Abstraction is not about hiding the internals of `HashMap`, but about separating implementation from API (contract), e.g. the `Map` API can be implemented by `HashMap` or `TreeMap`. – Andreas Jun 17 '16 at 18:11
  • @Andreas It's both. It's about separating a HashMap from a TreeMap; at a more granular level, it's also about separating a HashMap with a linked-list collision resolution strategy from a HashMap with some other resolution strategy; at an even more granular level, it's about separating a HashMap with a linked-list resolution strategy, in general, from one with _this specific_ linked list resolution strategy. I would say that my example is very much one of separating implementation from API (both the Map API and generally the HashMap API, which says nothing about, e.g., `HashMap.Entry.key`) – yshavit Jun 17 '16 at 18:48
0

Abstraction is an overloaded term.

Abstraction, in object oriented languages, basically means leaving away unnecessary details when modeling real world objects. You could also think of it as a simplifying process.

Abstraction, in computer science as a whole, also means hiding complexity by providing some sort of simpler interface. Your question seems to aim at "data abstraction" which means hiding the exact way data is represented with an abstraction layer. This could be e.g. the Number data type in databases. You just know it is a number, but not how it is stored on disk.

Abstraction sometimes is used equivalently to encapsulation, too.

sorencito
  • 2,517
  • 20
  • 21