• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enacpusaltion In Java can anybody explain with small practical example  RSS feed

 
Anurag Pathak
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is Anurag Pathak.

I have one doubt regarding OOPS Concept.

Can Any body explain Encapsulation in Java how it is achived through small practical example.

Looking forward for reply.

Thanks & Regards

Anurag Pathak

pathak_arg@yahoo.com

 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Singh wrote: . . . So we can say Abstraction is achieved by encapsulation.
Very loose use of words there; since abstraction and encapsulation are different it would be better to say that good encapsulation helps abstraction.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!