Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Override equals method

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please why i can't override an equals method with a class how is a subclass of class object?
 
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't understand the question. Do you mean, if I give an equals() method a parameter type other than object, isn't it overridden? Because the Java┬« Language Specification (=JLS) defines what is and what isn't overriding. Overriding requires the same method name and the same types of parameters. The equals() method can be overridden by a public (not static) method called equals() taking one parameter, type Object.
 
Rancher
Posts: 828
19
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
emma,

I am not sure what you are saying:



There is a very simple equals Override for Object , so simple in fact that it does nothing except return true for every case.  Could you please give us a small example of what you have tried--an example of what you tried works much better than my crystal ball does at this time.

A sub class is any class that is derived from an existing class: so see "extends".

Les
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Les Morgan wrote:. . . A sub class is any class that is derived from an existing class: so see "extends".

Les

. . . but don't forget that every class implicitly extends Object if you don't specify another superclass with extends.
 
Les Morgan
Rancher
Posts: 828
19
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,
Every class implicitly extends Object, unless you are explicitly extending Object. All classes are derived from Object.
Les

Campbell Ritchie wrote:

Les Morgan wrote:. . . A sub class is any class that is derived from an existing class: so see "extends".

Les

. . . but don't forget that every class implicitly extends Object if you don't specify another superclass with extends.

 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to say that can i override the equals method but i pass as argument a name of class extends of object  instead of object
 
Saloon Keeper
Posts: 3407
149
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would be overloading instead of overriding.

You can overload the equals method using a class other than Object as parameter, but that will not give the expected outcome everytime.
See this topic where a similar queation arose, where equals was overloaded instead of overridden, and what complications that may bring:

overloading instead of overriding
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

emma roberts wrote:. . . i pass as argument a name of class extends of object  instead of object

Make sure you are precise with your terminology. When you write the method, whatever goes inside the () is called a parameter, but in this case it must have the type Object:-What you have in other code where you decide what to send to a method is called an argument:-You can pass arguments of any type to equals() because everything can be converted to an Object, even primitives (boxing conversion), since every instance of every class IS‑AN Object.
That doesn't make any difference to whether the method is overridden or overloaded. What will cause trouble is overloading such a method in subtypes. If I wrote this sort of thing:-I would get different behaviour if I passed an argument type Foo from an argument type anything different. If I added such an object to a Collection and then tried to see whether the Collection contained that object, it might return false.
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I would only get false if I had two objects otherwise identical.If I override equals() correctly, line 239 will print true, but if I overload that method it will print false. Remember the List sees its elements as type Object, and it can only therefore see one equals() method.
So in the case of equals(), what matters is how the method is overridden, not what type of argument it is sent.
 
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

emma roberts wrote:please why i can't override an equals method with a class how is a subclass of class object?

every class is subclass to object class so equals() method you can implement to subclass but if you have hierarchy of subclasses then it is not possible to override equals() method from class which is superclass to the class in which equals() already implemented
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SH: please explain your last post. I am finding it difficult to understand, but I think it is incorrect.
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ER: Please tell us what you found when you read Bloch, Langer, and Odersky Spoon and Venners.
 
sohail hussain
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:SH: please explain your last post. I am finding it difficult to understand, but I think it is incorrect.

means if you have class c1 which has implemented equals() method from object class then another class c2 which extends c1 cannot override the equals method from c1 class to c2 class
 
Campbell Ritchie
Marshal
Posts: 65033
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it can override equals() from its superclass. It may introduce errors into the method by such overriding, but the only way to prevent such overriding is to mark the method final.
 
sohail hussain
Ranch Hand
Posts: 58
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, it can override equals() from its superclass. It may introduce errors into the method by such overriding, but the only way to prevent such overriding is to mark the method final.

I mean it's again depend on the type of access modifier using in equals() method                                            
Example... Default boolean equals() {}
Then you cannot override the equals()
This is from whizlabs mocks question which I got to know, and I personally checked it, compiled successfully,
So my question is why this diffrence has made for access modifiers, may be reason is package accessibility
 
Enthuware Software Support
Posts: 4329
35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

sohail hussain wrote: I mean it's again depend on the type of access modifier using in equals() method                                            
Example... Default boolean equals() {}
Then you cannot override the equals()
This is from whizlabs mocks question which I got to know, and I personally checked it, compiled successfully,
So my question is why this diffrence has made for access modifiers, may be reason is package accessibility


Don't know what whizlab says but you can't override equals in a subclass with less than public accessibility because it is public in Object class.
 
sohail hussain
Ranch Hand
Posts: 58
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:

sohail hussain wrote: I mean it's again depend on the type of access modifier using in equals() method                                            
Example... Default boolean equals() {}
Then you cannot override the equals()
This is from whizlabs mocks question which I got to know, and I personally checked it, compiled successfully,
So my question is why this diffrence has made for access modifiers, may be reason is package accessibility


Don't know what whizlab says but you can't override equals in a subclass with less than public accessibility because it is public in Object class.

yes you are right it should be public  accessibility
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!