Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Encapsulation" Watch "Encapsulation" New topic
Author

Encapsulation

Sharmistha Sarkar
Ranch Hand

Joined: Dec 18, 2012
Posts: 44
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 .
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Dec 18, 2012
Posts: 44
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.
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

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

Joined: Dec 18, 2012
Posts: 44
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..............
Peter Lager
Greenhorn

Joined: Nov 10, 2012
Posts: 6

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

Joined: Jul 26, 2007
Posts: 157
    
    1

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".
Vish Shukla
Ranch Hand

Joined: Oct 12, 2008
Posts: 111
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.


Thanks & Regards,
Vishal S Shukla (SCJP 93%, SCWCD 94%, SCBCD 100%)
 
 
subject: Encapsulation
 
Similar Threads
Use JAAS for access control.
Encapsulation
Oops concept
encapsulation & abstraction
What is the benefit of Encapsulation?