Tag Archives: Override equals and toString methods

Override the equals method and override the toString method

Override equals and toString methods


//Rewrute equals
public boolean equals(Object obj) {
	if (obj == null) {
		return false;
	}
	if (obj == this) {
		return true;
	}
	if (obj instanceof People) {
		People p = (People)obj;
		if (p.name.equals(this.name) && p.age == this.age && p.sex == this.sex) {
			return true;
		}
	}
	return false;
}
//Rewrute tostring
	public String toString() {
		return name+":"+age+":"+sex;
	}