Encapsulation


Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data it manipulates. Other way to think about encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield. Differences Between Abstraction and Encapsulation
In short, abstraction hides details at the design level, while encapsulation hides details at the implementation level.

Abstraction Encapsulation
Abstraction is a general concept formed by extracting common features from specific examples or the act of withdrawing or removing something unnecessary. Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse.
You can use abstraction using interface and abstract class. You can implement encapsulation using access modifiers (public, protected, and private).
Abstraction solves the problem in design level. Encapsulation solves the problem in implementation level.
For simplicity, abstraction means hiding implementation using abstract class and interface. For simplicity, encapsulation means hiding data using getters and setters.

Review: Abstraction and Encapsulation
    Which statement is NOT true about abstraction and encapsulation?

      Abstraction solves the problem in design level.
      Encapsulation means hiding data using getters and setters.
      Encapsulation solves the problem in implementation level.
      You can implement abstraction using access modifiers (public, protected, and private).
        Result:




      I was playing chess with my friend and he said,    
      “Let’s make this interesting.” So we stopped playing chess.    
      — Matt Kirshen