What's the difference between abstraction and encapsulation?

前端 未结 24 1129
攒了一身酷
攒了一身酷 2020-12-22 17:16

In interviews I have been asked to explain the difference between abstraction and encapsulation. My answer has been along the lines of

  • Abstraction<

24条回答
  •  误落风尘
    2020-12-22 17:56

    Its Simple!

    Take example of television - it is Encapsulation, because:

    1. Television is loaded with different functionalies that i don't know because they are completely hidden.

    2. Hidden things like music, video etc everything bundled in a capsule that what we call a TV

    Now, Abstraction is When we know a little about something and which can help us to manipulate something for which we don't know how it works internally.

    For eg: A remote-control for TV is abstraction, because

    1. With remote we know that pressing the number keys will change the channels. We are not aware as to what actually happens internally. We can manipulate the hidden thing but we don't know how it is being done internally.

    Programmatically, when we can acess the hidden data somehow and know something.. is Abstraction .. And when we know nothing about the internals its Encapsulation.

    Without remote we can't change anything on TV we have to see what it shows coz all controls are hidden.

提交回复
热议问题