• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Object equals method

 
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?

 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
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
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure 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.
 
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
Boost this thread!