• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in equals method parameter

 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic