File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes doubt in equals method parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt in equals method parameter" Watch "doubt in equals method parameter" New topic
Author

doubt in equals method parameter

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
In the following code


in the above code in equals method parameter why Object type parameter is used first and then later typecasted into Moof type in the if statement,
in place of "Object o" we could have directly given "Moof o" ??
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

What you're doing is overriding the equals() method from Object, and that's declared with an Object argument. So in order to override it you need to have the same signature. The equals method should be callable with any parameter.

You could define an equals(Moof) method. But that wouldn't be an override, and whike your test there might work, other code might not.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You should use the @Override annotation on methods that should be overriding superclass methods. For example:

When you do that, the compiler will check that the method is really overriding a superclass method, as you intended. If you would then write this:

the compiler would give you an error message, so that you know something is wrong with your code.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
jQuery in Action, 2nd edition
 
subject: doubt in equals method parameter
 
Similar Threads
Want hepl to understand Implementin an equals()
instanceof
Doubt about instanceof operator
Generics and collections K&B pg 526
instance of problem