Help coderanch get a
new server
by contributing to the fundraiser
  • 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

From Dan's Mock exams

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one of his exams, Dan says that



A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model



But i was wondering, is it not true that the accessor methods are normally used to validate the data ? Mutators or setters are used to set the data right?

Thanks for your responses.
Regards
Kavya
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Dan is trying to say is that, by using a Mutator or Setter, a class claims control over what it stores in its internal data model. Regardless of what you send to the method, the class gets the "final say" of what will be stored. Take this code, for example:



From this code, you can see that the class Encap is using a Setter method to verify that the value to be stored into its internal data structure is "acceptable." In this case, Encap won't allow negative values to be stored in internalValue - the Setter method allows it to maintain control over that.

If this class were not so tightly encapsulated, you could circumvent this logic. Here's the same code in a less tightly encapsulated version:



Certainly, if the users of this class invoke the setInternalValue method, that logic still applies. By making the member variable public, however, there's no guarantee that the logic within setInternalValue will be executed every time the member variable is changed.

I hope that helps,
Corey
 
kavya krushi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Corey ...
 
I do some of my very best work in water. Like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic