• Post Reply Bookmark Topic Watch Topic
  • New Topic

access to private fields by object of the same class  RSS feed

 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All at the Ranch:

I am working my way through Core Java Volume 1 8th Edition
by Cay Horstmann .

And I ran across something that made me think a bit.
That was a new sensation.

Anyway please take a look at this

class Employee {
....
boolean equals(Employee other) {

return name.equals(other.name);
}
private String name;
private double salary;
}


A typical call
if (harry.equal(boss)) .....

This method accesses the private fields of harry, which is not surprising.
It also accesses the private fields of boss. This is legal because boss is an object
of type Employee, and a method of the Employee class is permitted to access the
private fields any object of type Employee.

What if Employee.equals was declared private?
In the above can harry take a peek at boss's salary? I think so.

This was a little surprising to me.

Thanks for your attention.

Best Regards

KD






 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevind duff wrote:
What if Employee.equals was declared private?

Try it and see.

This was a little surprising to me.


What was surprising? You already said you understand that objects can access private members of other objects of the same class.
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My mistake this was quoted from the book.

" This method accesses the private fields of harry, which is not surprising.
It also accesses the private fields of boss. This is legal because boss is an object
of type Employee, and a method of the Employee class is permitted to access the
private fields any object of type Employee. "

yep guess I should give it a try.

KD
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are they really overloading the equals method rather than overriding it? Which chapter is that from? I have a different edition of Horstmann and Cornell, but the Employee class in my copy does not have that overloaded equals() method.
You do realise it is usually a serious design error to overload a public equals method like that?
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working with Core Java Vol I Fundamentals Eighth Edition
by Horstmann and Cornell

The example above is from pg 131

KD
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kevind duff wrote:I am working with Core Java Vol I Fundamentals Eighth Edition
by Horstmann and Cornell

The example above is from pg 131

KD


And...?

At the moment, it's not clear (to me at least) what point you're trying to make or what question you're asking.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
kevind duff wrote:I am working with Core Java Vol I Fundamentals Eighth Edition
by Horstmann and Cornell

The example above is from pg 131

KD


And...?

At the moment, it's not clear (to me at least) what point you're trying to make or what question you're asking.

I think he was replying to Campbell's question.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
I think he was replying to Campbell's question.


Ah, right, I see it now. That's what I originally thought, but I skimmed and didn't see the, "Which chapter is that from?" part. Thanks!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that page 132 or 133 contains a discussion of why the code on page 131 is a bad, bad idea?
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Is it possible that page 132 or 133 contains a discussion of why the code on page 131 is a bad, bad idea?
I hope so; in my copy (7th edition) it is about page 121, but it looks nothing like what was presented earlier.
 
Ryan Sykes
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any discussion of that being a bad idea in the 8th Edition. It moves on to other things after this example.

Being new to Java, I decided to google why this is a bad idea. I found this link to be very instructive: http://www.artima.com/lejava/articles/equality.html
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello All:

There is no discussion as to why this is a bad idea.
The text just moves on to the next topic.

So there is issue here, just not what I thought.

Thanks all


KD
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very surprised to see that sort of code in Horstmann and Cornell; they know better than to write such things.
Is the artima link Odersky, Spoon and Venners? That is one of the classic references about the equals method; you will find a couple of others mentioned here.
 
Ryan Sykes
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I am very surprised to see that sort of code in Horstmann and Cornell; they know better than to write such things.
Is the artima link Odersky, Spoon and Venners? That is one of the classic references about the equals method; you will find a couple of others mentioned here.

Yup, it is indeed the link to the article by Odersky, Spoon and Venners. It was a lovely read and very accessible to a java novice.
 
Campbell Ritchie
Marshal
Posts: 56596
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed to find a copy of Horstmann and Cornell 8th edition. It says this on page 131
For example, consider a method equals that compares two employees.
. . . which is followed by the method shown at the beginning of this thread. I do not think Horstmann and Cornell are recommending such an equals method, but using it to show access to private fields. There is more discussion of the equals method on pagef 193ff; page 196 describes an equals method identical to page 131 as “a common mistake”.

So I think H&C stand vindicated, as I thought earlier they would have been
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!