Simple way to understand Encapsulation and Abstraction

后端 未结 15 1212
谎友^
谎友^ 2020-11-27 09:47

Learning OOP concepts especially interested to understand Abstraction and Encapsulation in depth.

Checked out the below already

Abstraction VS Information Hi

15条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 10:38

    Abstraction is like using a computer.

    You have absolutely no idea what's going on with it beyond what you see with the GUI (graphical user interface) and external hardware (e.g. screen). All those pretty colors and such. You're only presented the details relevant to you as a generic consumer.

    Encapsulation is the actual act of hiding the irrelevant details.

    You use your computer, but you don't see what its CPU (central processing unit) looks like (unless you try to break into it). It's hidden (or encapsulated) behind all that chrome and plastic.

    In the context of OOP (object-oriented programming) languages, you usually have this kind of setup:

    CLASS {
      METHOD { 
        *the actual code*
      }
    }
    

    An example of "encapsulation" would be having a METHOD that the regular user can't see (private). "Abstraction" is the regular user using the METHOD that they can (public) in order to use the private one.

提交回复
热议问题