• 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

Design Problem (Data Hiding & Encapsulation)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I was asked following question in interview.

I have following two options for designing a class:

A) Provide a constructor which takes all the values of instance level variable and set them in constructor (class has some attributes).

B) Provide getter/setter for all the instance level attributes.

Which one will be a better option from Data Hiding/Encapsulation point of view?

Thnaks a lot,
Deepu
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Option A
 
Deepu Gupta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for reply!!

Could you please let me know the reason as well? Are you assuming the Class with parmeterized constructor does not have getter/setter?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Deepu Gupta:
Could you please let me know the reason as well?



Well, one could argue getter/setter methods violate encapsulation principles, because they exists solely for the purpose of directly manipulating an object's internal state. Strictly speaking an object should only expose behavior that operates on its state on behalf of a client, instead of allowing the client to shuffle it's state field by field by means of accessor methods (possibly violating class invariants). However, in my opinion that's a very academic viewpoint. For example, the amount of libraries and tools alone that depend on the JavaBeans convention to function properly (or at all) makes strict adherence to this principle a virtual impossibility.
[ September 22, 2008: Message edited by: Jelle Klap ]
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Jelle has summed it up beautifully and I absolutely agree.

Your original question was

Which one will be a better option from Data Hiding/Encapsulation point of view?


Purely from this point of view I chose option A. After all this was an interview question and nothing to do with real life!
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic