Amazon Interview Question
Software Engineer / DevelopersCountry: United States
encapsulation is one of the four fundamentals of OOP (object-oriented programming). Encapsulation is to hide the variables or something inside a class, preventing unauthorized parties to use. So the public methods like getter and setter access it and the other classes call these methods for accessing
Basically, as what its name implies, encapsulation means
1) grouping related data and subroutines together.
2) hiding irrelevant information from users.
3) exposing nessary interfaces to users.
For example, class is one of constructs that 1) group related data and subroutines into objects. 2-3) hide implementations and expose interfaces by access control(visibility of its members and of its base-class members).
Besides, in the old day, module is the construct that provides encapsulation for non-OOP languages. Like in C, a compilation unit is the body of a module that 1) groups data and subroutines. It 2-3) hides implementation details in the body and exports interfaces via its header.
Two parties involved: Class designer and Client code. Users ( client ) of the class ( designed by class designer) is unaware of the complexity of the task undertaken by the class. Encapsulation defines clean dispersal of responsibilites, with each party unaware of how other party handles the given task, with all parties interdependent in accomplishing a common goal.
- information hiding
- peter February 13, 2013- expose interface
- achieved by creating classes
- The inclusion within a program object of all the resources need for the object to function