Anurag Pathak wrote:Can Any body explain Encapsulation in Java how it is achived through small practical example.
In principle any class is an example. When you encapsulate something you get an inside and an outside and that's what happens when you introduce a class. The outside is called a data abstraction or the type. Hidden inside is the implementation.
The class allows you define a little "capsule" with specific properties you think it should have. People can use it without having to bother about how it works inside.
But also primitives like int and double, etcetera, are examples of encapsulation. It's just that they're readymade. The class concept (including enum) allows you to create your own "capsules".
Anurag Pathak wrote:
Can Any body explain Encapsulation in Java how it is achived through small practical example.
A simple pojo class with some getter and setters. Represents Encapsulation.
Difference from Abstraction.
list is an List object. So the User doesn't know about the inner implementation details of List
But we can perform many actions with this object. Like:-
So the List class is capsuled with these Methods.
Where as while using it in our programs we are abstracting those methods.Unaware of the inner details.
So we can say Abstraction is achieved by encapsulation.