Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Encapsulation...

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3. Which is the advantage of encapsulation?
1)Only public methods are needed.
2)No exceptions need to be thrown from any method.
3)Making the class final causes no consequential changes to other code.
4)It changes the implementation without changing the interface and causes no consequential changes to other code.
5)It changes the interface without changing the implementation and causes no consequential changes to other code.
Hi,
The above is a question from the Sun's sample questions.Option 4 and 5 are not very clear to me .Would be obliged if somebody could explain them to me.
Thanks
Ira
 
Enthuware Software Support
Posts: 4637
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you have to understand what encapsulation means...in a nutshell it means, the user of a component is provided with the functionality but the user is not told how that functionality is actualy provided/implemented internally by that component. If we go deep, this essentially means the fields of a class (which probably may hold some data, some state information or whatever) are not exposed to the user. But public methods are provided which let the user access those fields.
Let's take an example, you may write a class that represents an Employee. Now, there can be a lot of fields in the class like name, age, experience, etc. There may be a method 'getRating()' which rates an employee for appraisal purposes. Let's say this method is implemented such a way that it returns rating based on just experience.
Now, there are two options
1. just provide getRating() method and do not expose the experience field.
2. expose the experience field, and then there is no need to write getRating() method as the user of this class can calculate the rating himself!
Suppose we choose option 2 and other classes start using the experience field as they please....an year down the line, manangement says that rating should not just be based on experience but also on last year's performance!
What happen's to all that code that made use of just the experience field to calculate the rating? You have to change all that code.
Had we chose option 1, we would only have to change the method getRating() and that's it, we are done. This is what is meant by encapsulation. We encapsulated ( hid it from the user) the internal details ( ie. the field experience) and exposed just the functionality (ie. the method getRating() )
Now it's easy to analyse the given choices....
4)It changes the implementation without changing the interface and causes no consequential changes to other code.
We saw that we changed the implementation of getRating() method and nothing else broke!
5)It changes the interface without changing the implementation and causes no consequential changes to other code.
No, it did not change the interface, other classes will still use the same method getRating().
HTH,
Paul.
------------------
Get Certified, Guaranteed!
http://www.enthuware.com/jqplus
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic