Overriding equal method
In java when we use equals method to compare two objects it returns true only if these referring to same memory location. Code public class OverrideEqual{ public static void main(String[] args){ Person p1= new Person("John",17); Person p2= new Person("John",17); if(p1.equals(p2)){ System.out.println("p1 equals to p2"); }else{ System.out.println("p1 is not equals to p2"); } } } class Person{ String name; int age; Person(String name,int age){ this.name=name; this.age=age; } } Output: p1 is not equals to p2 So if we want to compare two objects according to the states or values then we have to override the equals method. Code: public class OverrideEqual{ public static void main(String[] args){ Person p1= new Person("John",17); Person p2= new Person("John",17); if(p1.equals(p2)){ System.out.println("p1 equals to p2"); }else{ System.out.println("p1 is not