/*
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));
}
}
Asked
Active
Viewed 22 times
0
-
1There is a typo, you want either `this.cName = sName;` or `Car (String cName, int cModel)` . – Arnaud Nov 14 '17 at 07:40
-
why creating pointless local variables? – Stultuske Nov 14 '17 at 07:41