aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object equals method" Watch "Object equals method" New topic
Author

Object equals method

fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
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

Joined: Jul 16, 2010
Posts: 75
But return type can be changed if we are using covariant returns
fu bace
Greenhorn

Joined: Oct 16, 2009
Posts: 24
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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

Joined: Jul 16, 2010
Posts: 75
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

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
for primitives return types

overriden and overriding method should have exactly same return types.

SCJP 6.0 96%

(Connecting the Dots ....)
 
Consider Paul's rocket mass heater.
 
subject: Object equals method