• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encapsulation

 
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , Can someone please explain me in which situation of some business requirement I should use encapsulation? So far I came to know; encapsulation allow read-only data. It declares data as private so that no one can access it but access it in the same class via public method. Si it explains How to use encapsulation with syntax. But I want to know if I get any requirement for example develop a "Bank application" . I need Login page , then get the data from user, validate data, store in db then display informations to end user etc. In this scenario where I can use encapsulation concept? why should I use encapsulation only? You can give me some other example also .
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Sharmistha Sarkar

Sharmistha Sarkar wrote:Can someone please explain me in which situation of some business requirement I should use encapsulation?


*Head First Design Pattern* book explained well on encapsulation in strategy pattern(#1 chapter).
 
Marshal
Posts: 79178
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sharmistha Sarkar wrote: . . . in which situation of some business requirement I should use encapsulation? . . .

Simple answer: always.

Slightly less simple answer: have a quick look at this Wikipedia link Encapsulation_(object-oriented_programming), which explains it simply.
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seetharaman and Campbell, I have gone through the book and the wiki... So "hiding information" is encapsulation. For example, we do hide some methods or some stuffs which are irrelevant for others [I am hiding my heart from the world . This is encapsulation ]. So here we go , Abstraction is also hiding irrelevant information from the world and shows necessary information[My face is visible but i am alive for my heart. so 'face visibility and hiding my heart' together defines abstraction . Hiding my heart from outside world which is encapsulation concept too ]. So encapsulation comes under abstraction concept, isn't it? I have gone through few more links and found abstraction vs encapsulation "http://www.dotnetfunda.com/articles/article632-what-is-an-abstraction-.aspx" under topic "Difference between Encapsulation and Abstraction" : Abstraction solves the problem in the design level where as Encapsulation solves the problem in the implementation level. I also found in some other link abstraction happens with abstract class and interfaces . Encapsulation happens with private access specifier , accessor and mutator . Using private keyword before any variable or using accessor and mutator, I am not really finding an interest in encapsulation concept. Is it possible to give me a real example [if necessary with code implementation or by mentioning business logic] where I can say , yes , I need strongly encapsulation concept...it worth here.
 
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So encapsulation comes under abstraction concept, isn't it?



I really don't think so. In abstraction i am hiding complexity from user, or internal working, while during encapsulation i am binding my data and operations, i.e. hiding data itself.
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok , thats sound good. Harshvardhan , if you don't mind could you please elaborate on how you achieve this? I mean, some detail on the business logic [story]; which you have implemented..............
 
Greenhorn
Posts: 10
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the Bank application as an example assume you create a class called Account which has a public field called accNbr (the account number). Because it is declared public then it can be changed from anywhere in the entire bank application software. What happens if the actual number is wrong for instance it has only 7 digits when the bank uses 8 digits for account numbers - the account has become 'invisible'. If the field had been declared private (encapsulated) in the Account class with a public method setAccNbr e.g.
public boolean setAccNbr(long accNbr) { ...
then this method could be used to validate the parameter, change the field only if it is ok and pass back true/false depending if it was valid.
It means that the whole bank application must use this method to set the account number so ensuring that it is valid.
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While i am working on my calculator, i don't need to bother how it is going to calculate. This is abstraction.

After i entered two numbers to give me sum, this is responsibility of my program to protect my inputs from accidental modification this is "encapsulation".
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Encapsulation has to do with hiding data, give it a name(private data members in a class - Car) and binding behavior methods(start, stop, accelerate) with it. These behavior methods will mutate or provide access to those data variables.

Abstraction provides perspective of the client in abstract terms. As a concept or an idea. This generalization allows clients to ONLY focus on What they are concerned with. Car is concrete entity where as Drivable, Trackable(which has position and can be tracked down) can be abstraction for Car for different clients.

You can check some real life examples of Abstraction and Encapsulation here.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic