0
/*
This is the Car class where the equals method of Object class is overridden to compare contents of 2 objects instead of comparing reference variables of 2 objects
*/
package langPackage;
public class Car 
{
    private String cName;
    private int cModel;

    Car (String sName, int cModel)
    {
        this.cName = cName;
        this.cModel = cModel;
    }

    public boolean equals(Object Obj1)
    {

            String s1 = cName;
            int r1 = cModel;

            Car c = (Car) Obj1;

            String s2 = c.cName;
            int r2 = c.cModel;

            if((s1.equals(s2)) && (r1==r2))
            {
                return true;
            }
            else 
            {
                return false;
            }




    }
    public static void main(String[] args) 
    {
        Car c1 = new Car("Ciaz",2018);
        Car c2 = new Car("Celerio",2017);
        Car c3 = new Car("Ciaz",2018);
        Car c4 = c1;

        System.out.println(c1.equals(c2));
        System.out.println(c1.equals(c3));
        System.out.println(c1.equals(c4));
    }

}
Eran
  • 387,369
  • 54
  • 702
  • 768
Tej
  • 11
  • 4

0 Answers0