Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Encapsulation in java

 
Ranch Hand
Posts: 214
Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I faced a question yesterday during my technical interview the code is below:





The question was of the interviewer that , Is this class is purely encapsulate?

and my answer was it is not encapsulate if we use private void show() instead of "public void show()". Am I right? Please explain this topic..

It will be very highly appreciate.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not the visibility of the show() method that matters. The point is the a member variable. That's visible to other classes (in the same package). To be encapsulated, it should only be possible to access member variables via a method.

E.g. change it to:
 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you are right, nothing set to private there the Class is automatically public if not marked otherwise, and the int as well and the method is set Public. So no data hiding at all.

 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mike ryan wrote:... nothing set to private there the Class is automatically public if not marked otherwise...


No. The Class has default (package) access if not marked with any modifier.
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, if the variable is not marked, it is "package protected", not public, and so the original reply was correct -- this is visible only to other classes in the package.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's debatable whether just providing public (or package) accessors (getA and setA) to a private member is considered more "encapsulated" than just providing a public (or package) attribute.

It becomes really encapsulated when you provide a getter for an attribute and that value of the attribute is calculated by a method, and not provided from an outside call, so that no one messes with it.


Now the class has complete control over its private member "a".

Mathieu
http://simpleadn.blogspot.com

 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing more "completely encapsulated" about a variable that is computed instead of stored in the same format as being returned. The class has complete control over 'a' in relation to other classes, because all they can do are the operations provided by the class for a -- whether it is computed, stored, generated at random, or whatever is up to the class providing those methods. That is complete, 100% encapsulation in relation to other classes. In circumstances where they cannot see the code that returns a, they would not know nor should they care how the value is determined, or what else is done when it is determined, etc.

The thing that makes this "more encapsulated" than a public or package protected (or protected) member is that, once you reveal that internal representation of that data, and publicize it as part of the way another class can interact with your class, then changing it has the potential for changing any class that uses this class. Therefore the using class and the used class are tied together by this implementation, and changing it could require changing all.

In addition, if you reveal a variable's type and make it accessible directly, you lose the opportunity for the getter / setter methods to do something in addition -- update counters, update collections, notify listeners -- that they may not do when the class is written, but someone may find a use for later on.

rc
 
Marshal
Posts: 76483
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ralph Cook wrote: . . . it is "package protected", not public, . . .

Don't you mean "package private"?
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, something like that

(sorry for the terminology confusion -- I SAID it wasn't used often...)

rc
 
He baked a muffin that stole my car! And this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic