• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object equals method

 
fu bace
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i expected an output

false true

but the output is

false false


I know that if a subclass doesnt override the equals method of the Object class that class uses the equals method of Object class but here Test class is overriding the equals method.

So?

 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fu bace wrote:
I know that if a subclass doesnt override the equals method of the Object class that class uses the equals method of Object class but here Test class is overriding the equals method.

So?


To override a method, you need the same signature. If not, you are just overloading the method.

Henry
 
fu bace
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object class has a method

boolean equals(Object obj)

and has a signature of equals(Object obj)

in my code equals method has a signature of

equals(Team t)

Ok I got it. Thanks

btw

"To override a method, you need the same signature."

this sentence is half correct. Should be

"To override a method, you need the same signature and return type"

cause return type does not count in signature.
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But return type can be changed if we are using covariant returns
 
fu bace
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshit Sethi wrote:But return type can be changed if we are using covariant returns


Compiler gives an error if you do something like

int equals(Object o)
 
Henry Wong
author
Marshal
Pie
Posts: 21016
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fu bace wrote:
Harshit Sethi wrote:But return type can be changed if we are using covariant returns


Compiler gives an error if you do something like

int equals(Object o)



The key phrase in Harshit Sethi response is "covariant returns" -- boolean and int don't fulfill the requirement.

Henry
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah and by covariant returns i mean that you can declare the return type to be the same as that of superclass' method or a subtype of that and that's possible if you are returning a reference.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshit Sethi wrote:yeah and by covariant returns i mean that you can declare the return type to be the same as that of superclass' method or a subtype of that and that's possible if you are returning a reference.


Exactly. That's not for primitives.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for primitives return types

overriden and overriding method should have exactly same return types.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic