Friday, May 20, 2011

Overriding the Overloaded

Most of the java programmers would have faced this problem, like i did.

I was reading about Collections and there was a line in double quotes in that web page..

"If you want to use equals() method for your class, you have to write hashCode() also".....

An any developer would do, i started writing a program to try this and i wrote following code

public class Emp{
private String Name;
private int code;

public int hashCode(){
return code;
}

public equals equals(Emp that){
return that.code == this.code;
}

}

COMPARISON NEVER WORKED ....

Can somebody explain me the reason ???????????????