Two Laptop Bag
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

doubt in equals method parameter

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 153
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

Joined: Apr 06, 2010
Posts: 4543

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: 15084

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 8 API documentation
It is sorta covered in the JavaRanch Style Guide.
subject: doubt in equals method parameter
It's not a secret anymore!