• 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

Encapsulation in class?

 
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We know that encapsulation is about binding data and functions together into a class. That is fine with private primitive variables and public methods as in Example 1 and this example represents a fully encapsulated class.

My question is : In example 2 shown below one object is referring another object. Is Example 2 also an example of fully encapsulated class. I am asking this question because the  data and functions are not completely specified in the same class. Infact it has a reference to another class named Address.

Example 1:


Example 2::
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:. . . Is Example 2 also an example of fully encapsulated class.  . . .

No. Neither example is fully encapsulated; both sacrifice encapsulation if they have the setXXX methods because they allow the state of the object to change. Full encapsulation and the bean pattern which requires the setXXX and getXXX methods are mutually incompatible.
Find a copy of Effective Java by Joshua Bloch and find the chapter about defensive copies. You will then find out that the difference is not between primitives ad reference types, but between mutable and immutable fields. Strings are immutable, so the state of the String can no more change than the state of an int. If you return a String reference, you cannot get any problems (unless somebody is doing some very naughty reflection ‍). If however, you return a mutable reference type (e.g. an array), the state of that array can be changed elsewhere and that changes the state of your current object.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether you will have problems with encapsulation with a getAddress method depends on whether the address class is mutable or immutable.
 
raja singh kumar
Ranch Hand
Posts: 212
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give a simple example of a fully encapsulated class?
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.String
You can find its code in the src.zip file inside your standard Java® installation folder.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

raja singh kumar wrote:Can you give a simple example of a fully encapsulated class?


Well here's my attempt, although many will disagree:You may also find this article worth a read, because there's more to encapsulation than simple data hiding.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . Well here's my attempt, . . .

Mr Chad wrote:Wot no constructor?

If you don't write a constructor, you are going to have the default values for those two fields. I think I would use LocalDate rather than the old date API. Of course, LocalDate has the advantage of immutability, so you can have no problems about defensive copies. Agree with you about marking that class final, in which case it is unnecessary to mark the method final.If you use a mutable class like Date, you would have to take a copy:-I don't usually override clone() methods, but Date already has a clone() method, so I might as well use it.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Agree with you about marking that class final, in which case it is unnecessary to mark the method final...


Oooh no. The reasons for putting final on a method are NOT the same as they are for a class, and the problem with leaving out the latter is that if, later on, I decide to add an Employee class, I can't rely on methods that I don't want overridden being so.

Winston
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. My mistake.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may Help!! Encapsulation
Thankyou!
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

The examples I have seen on that website have been uniformly poor quality and unhelpful because of lack of explanation. The example you showed there is a good example of how not to do it
 
Shiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic