• 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
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

overidden equals() error

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am working on,overriding the equals() method.The above equals() method (o instanceof NewCar) will return false.ithought that way.but the compiler showing,incovertible types error(Found bject,Required:NewCar).why is the error coming.Please help.
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the class definition?
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure Keith.But i got rid of that error.it was just an IDE error.But when i run the above program,overridden equlas() method,the first condn ((o instanceof NewCar)
is false .if the first condn is false,next && no matter about what the next condn is it is skipped and the result of if condn is false.so returning false.i thought that way.but it seems like it is returning true.please help again on this.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think the first condition is false?

The first condition is o instanceof NewCar. You are sending a reference to a NewCar instance to the equals method.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Ouput :who.Thats why i am telling the first condition is false on the above program.
[ January 16, 2007: Message edited by: Shiva Mohan ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone please help me on this?it would be so much helpful for my understanding.
Thanks.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


When testing the equality of two objects use '.equals()' instead of '=='.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NewCar extends Object class .(infact every class extends Object class).
The NewCar object can be referred by superclass's refernce according to polymorphism.here, you are using superclass refernce 'o' to refer a subclass object NewCar.(polymorphism)
so there is no need of explicit casting in this case. o instanceof NewCar should return true.

i don't understand the second one...(object23....)
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:


Ouput :who.Thats why i am telling the first condition is false on the above program.

[ January 16, 2007: Message edited by: Shiva Mohan ]



o is not an instance of Objective23. It is an instance of NewCar. That's why the first condition is true.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


still output:who(false part is working for me)

How the polymorphic approch is coming here.when will we use the objectreference converion and explicit casting rules on instanceof operator..
i always works like this ---->o instanceof NewCar means NewCar=(NewCar)o; what i am looking for means is the Object(o referencetype) holds all the features of cating type(NewCar).no so false.
please explain instanceof on when we use conversion rules and explicit casting rules.i have already read tons of materials.but still not cleared.please somebody help me based on my understanding.
 
Keith Lynn
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Shiva Mohan:


still output:who(false part is working for me)

How the polymorphic approch is coming here.when will we use the objectreference converion and explicit casting rules on instanceof operator..
i always works like this ---->o instanceof NewCar means NewCar=(NewCar)o; what i am looking for means is the Object(o referencetype) holds all the features of cating type(NewCar).no so false.
please explain instanceof on when we use conversion rules and explicit casting rules.i have already read tons of materials.but still not cleared.please somebody help me based on my understanding.



In the code, o is not an instance of anything.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Keith.Can you give one example of instanceof with polymorphic approch with conversion rule on one thing and explicit casting rule on one thing.meena's reply is still confusing me.one example would be very much helpful with detailed explanation.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



please help me.sorry for bringing the same doubt again.But still don't get.

[ January 17, 2007: Message edited by: Shiva Mohan ]
[ January 17, 2007: Message edited by: Shiva Mohan ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

since the (myn.o instanceof NewCar) returns false,i am telling the first condition of (o instanceof NewCar)is false.


public boolean equals(Object o) {
if ((o instanceof NewCar)
&& (((NewCar) o).getOwner().equals(owner))
&& (((NewCar) o).getColor().equals(color)))
return true;
else
return false;
}



please explain if the first condition of quote that is (o instanceof NewCar) is false or true.if true means can anyone please explain it clearly for me.
    Bookmark Topic Watch Topic
  • New Topic