7

Please look at this link of Joshua Bloch's Effective Java.

In second paragraph, the author says:

The class is private or package-private, and you are certain that its equals method will never be invoked. Arguably, the equals method should be overridden under these circumstances, in case it is accidentally invoked:

@Override public boolean equals(Object o) {
     throw new AssertionError(); // Method is never called
}

Please explain this. I am getting confused by the author's use of term private class and that why is there a need to override equals method when we know for certain that it won't be invoked.

Tom
  • 16,842
  • 17
  • 45
  • 54
user961690
  • 698
  • 1
  • 11
  • 22
  • 1
    Cannot access that page. Report the important part. – V G Mar 23 '15 at 10:57
  • 1
    Sir, here is another link to this article http://jtechies.blogspot.in/2012/07/item-8-obey-general-contract-when.html\ – user961690 Mar 23 '15 at 10:59
  • 2
    You: *"when we know for certain that it won't be invoked"*; Joshua Bloch: *"in case it is accidentally invoked"*. Do you have another question? – Tom Mar 23 '15 at 11:00
  • Sir, I had to ask that when the class is package private, it means it can be accessed in its package only and not outside. And since, we have coded the package ourselves, we will never invoke the method in our code. Outside the package, it will never be invoked. So why do we need to override the method to throw an assertion error – user961690 Mar 23 '15 at 11:04
  • 3
    @user961690 You shouldn't trust yourself that much. Everyone makes mistakes and even if you have coded that class yourself, it is still possible that you call `equals`, because you just forgot, that you shouldn't do that. Especially if your last work on that class was several months ago. – Tom Mar 23 '15 at 11:58

1 Answers1

8

A class can be private only if it is an inner class.

As for the "why" is there a need to override equals, the reason is that by writing it as you have shown you will ensure that the method is never called intentionally. The moment six months in the future, when a new developer on the project will call equals on that class, the method will throw and signal that it is not correct to call it. That's a good thing; it prevents "forgetting" about it.

Andrea Bergia
  • 5,502
  • 1
  • 23
  • 38
  • 1
    Your first statement is inaccurate. Not only does the class not need to be inner (nested classes can certainly be private), but it can also be a top-level class in the same compilation unit as another class. – chrylis -cautiouslyoptimistic- Mar 23 '15 at 11:22
  • You're correct with regards to nested classes; I wrote "inner" but I meant both. However I can't seem to make your second case work: [this gist](https://gist.github.com/andreabergia/6aeb123b7474a5822ef6) when saved as MainClass.java doesn't seem to compile. – Andrea Bergia Mar 23 '15 at 11:23
  • 1
    The quote from the book in question is *"private or package-private"* so a package-private top-level class in the same compilation unit applies here too. – Radiodef Mar 23 '15 at 23:04