Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Encapsulation in java  RSS feed

 
Rd Dari
Ranch Hand
Posts: 214
Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • 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:
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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.

 
Vijitha Kumara
Saloon Keeper
Posts: 3997
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • 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.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • 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.
 
Mathieu Fortin
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
yeah, something like that

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

rc
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!