Encapsulation in Java , Difference between Encapsulation and Abstraction

What is Encapsulation in Java?

Encapsulation in java is a principle by which data is hidden in an Object and can be accessed by the outside world only through accessor methods. The simplest way to understand encapsulation is to look at the basic data types such as Integer or Double. These data types hide the actual implementation of the integer or double and exposes methods that the user can use without worrying about details such as how many bytes the integer or double uses. In more broader terms, a class encapsulates its state and exposes methods to the outside world to perform operations on its state.

What is difference between Abstraction and Encapsulation in Object Oriented Programming?

Abstraction refers to separating method definition from its implementation whereas encapsulation refers to hiding the state of an object. It is quite easy to understand if you think of abstraction as dealing with behaviour and encapsulation as dealing with data. In java, abstract classes and interfaces represent abstraction and the private variables and their getter/setters represent encapsulation.

Leave a Reply

Your email address will not be published. Required fields are marked *