Monday 25 July 2011

problem with equals( ) method .

when we generally read Object class, their we read that equals( ) works when , the two reference are pointing to one object .

animal a1 = new animal ( );
animal a2 = new animal ( );

these two are pointing to two different thing , its ok .

animal a3 = new animal ( );
animal a4 = a3 ;

here a3 , a4 are pointing to same object so they are equal .
now listen . a problem with this .
think that you have a car , and a key .
here are three things in use .

1.  person .
2.  key .
3.  car .

now think that we are doing searching , we want a person from car and key , so here we , our person and key are inherited from car . so for this first check instance of . and then apply the == equals method .
and then what you want .

No comments: