What's the difference between abstraction and encapsulation?

前端 未结 24 1131
攒了一身酷
攒了一身酷 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 18:11

    My opinion of abstraction is not in the sense of hiding implementation or background details!

    Abstraction gives us the benefit to deal with a representation of the real world which is easier to handle, has the ability to be reused, could be combined with other components of our more or less complex program package. So we have to find out how we pick a complete peace of the real world, which is complete enough to represent the sense of our algorithm and data. The implementation of the interface may hide the details but this is not part of the work we have to do for abstracting something.

    For me most important thing for abstraction is:

    1. reduction of complexity
    2. reduction of size/quantity
    3. splitting of non related domains to clear and independent components

    All this has for me nothing to do with hiding background details!

    If you think of sorting some data, abstraction can result in:

    1. a sorting algorithm, which is independent of the data representation
    2. a compare function, which is independent of data and sort algorithm
    3. a generic data representation, which is independent of the used algorithms

    All these has nothing to do with hiding information.

提交回复
热议问题