Arpit Saxena
Arpit Saxena

Reputation: 11

How does abstraction work practically? If a developer can navigate the code and see everything, then how does 'hiding' work in the real world?

I get confused when I read 'you are hiding concrete classes by using an interface'. Since when the app is used by another guy, he can see the entire code (unless I expose web services only...that is a different aspect, not to mix here)?

I agree that interfaces and abstract classes help to nicely design code, but from what I understand , that's it. I can't understand how practically in real world we are hiding stuff from others or adding more security etc.

Upvotes: 1

Views: 323

Answers (1)

kaya3
kaya3

Reputation: 51034

You have a misconception that the reason for "hiding" something is security. That is not at all what "hiding" means in this context. An interface "hides" the implementation details of a class in the same way a car's engine is "hidden" under the hood. Of course this doesn't stop you from looking at the engine if you want to see the car's internal workings, but the important points are that

  • You don't have to look at the engine or understand it in order to use the car, and
  • A car manufacturer doesn't have to make the engine work a particular way in order for the car to work as expected.

Likewise, by hiding the internal workings of a class, users of the class only need to understand how the class is meant to be used (i.e. its interface), and the developers of the class are free to change internal details of the class without annoying the users or breaking their code.

Upvotes: 6

Related Questions