aspose file tools*
The moose likes Java in General and the fly likes whats wrong with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "whats wrong with my code?" Watch "whats wrong with my code?" New topic
Author

whats wrong with my code?

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

error is


which is not true it should return true because the contents are same , where i m making mistake


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Vijay Tidake
Ranch Hand

Joined: Nov 04, 2008
Posts: 146

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

The important thing is not to stop questioning.Curiosity has its own reason for existing.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

And don't do this:

Do:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3568
    
  15
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.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Jul 03, 2011
Posts: 692

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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Right.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: whats wrong with my code?