Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

whats wrong with my code?  RSS feed

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
error is


which is not true it should return true because the contents are same , where i m making mistake
 
Ranch Hand
Posts: 148
Hibernate Java Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This error is due to the code


Dog(int j)
{
//From here the call to Animal() is made implicitly



To resolve error either add Animal default constructor or call super(<argument>) from above place.
Thanks
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vijay Tidake wrote:This error is due to the code


Dog(int j)
{
//From here the call to Animal() is made implicitly



To resolve error either add Animal default constructor or call super(<argument>) from above place.
Thanks


appreciate your work
but i have one more problem in the code is
when i changed the class type of a reference variable like and changed the below line to

then it gives me the following output

the output should be
d1 is not equals to d2
d1 is equals to d2
because the contents are same, tell me where i m wrong
 
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add the @Override annotation to your equals method, compile again, and see what happens. Hint: what is the parameter type required for Object.equals?
Also, your implementation is faulty, as it doesn't handle the cases where the argument is either null or an instance of any other class than Dog. In your case you will get a NullPointerException / ClassCastException; you should get false however.

By the way, if I take your original code, fix the compiler error by adding super(j) to the Dog constructor and run it, I get "not equals, equals" as you expect.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And don't do this:

Do:
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Add the @Override annotation to your equals method, compile again, and see what happens. Hint: what is the parameter type required for Object.equals?
Also, your implementation is faulty, as it doesn't handle the cases where the argument is either null or an instance of any other class than Dog. In your case you will get a NullPointerException / ClassCastException; you should get false however.

By the way, if I take your original code, fix the compiler error by adding super(j) to the Dog constructor and run it, I get "not equals, equals" as you expect.


it just to see my concept are right so it doesn't handle the cases where the argument is either null or an instance of any other class than Dog.
and as far as Hint is consider , the parameter of equals method has a reference variable a of class type Animal , so what if i call that method equals with a reference variable d1 of class type Animal it should produce the same output , right ?
because now both variable a and d1 refer to same object ie. new Dog(); the 1st object where as the parameter i m passing is d2 which refer to other , new Dog() object to check the content of that object whether it match the contents of 1st object or not
so then why it is not returning true value ?

and also writing @Override produce a compile time error

like this
 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naved momin wrote:and also writing @Override produce a compile time error


Exactly. That is why Rob suggested you do it. You are not overriding the equals method - you are overloading it.
 
Rob Spoor
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

naved momin wrote:so then why it is not returning true value ?


Because d1 is an Animal reference, and therefore it's trying to call the equals(Object) method. And you haven't overridden that method.


and also writing @Override produce a compile time error


Which means that you didn't override the equals method but overloaded it instead. What Joanne already said
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:

naved momin wrote:so then why it is not returning true value ?


Because d1 is an Animal reference, and therefore it's trying to call the equals(Object) method. And you haven't overridden that method.


and also writing @Override produce a compile time error


so basically you mean to say is that because i have written Animal a in the parameter of equals() which should contains Object obj as a parameter in equals() method , so that it could override the equals() method in object class
but now it is getting overloaded because i have used different parameter like Animal a in the equals() method , so because of different parameters of equals method it is getting overloaded not overriden , right ?

 
Rob Spoor
Sheriff
Posts: 21464
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!