Skip to content

Instantly share code, notes, and snippets.

@MykolaBova
Created October 1, 2016 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MykolaBova/5ff8e799f9dce771441ca3698b8d37ca to your computer and use it in GitHub Desktop.
Save MykolaBova/5ff8e799f9dce771441ca3698b8d37ca to your computer and use it in GitHub Desktop.
class Hash2 {
public static void main(String[] args) {
Car car1 = new Car(12345);
Car car2 = new Car(12345);
Car car3 = new Car(789);
System.out.println(car1.hashCode());
System.out.println(car2.hashCode());
System.out.println(car3.hashCode());
}
}
class Car {
protected int speedMax = 100;
private long id;
public Car(long id) {
this.id = id;
}
public Car() {
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + speedMax;
return result;
}
@Override
public boolean equals(Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
Car car = (Car) o;
if(id != car.id) return false;
return true;
}
public int getSpeedMax() {
return speedMax;
}
public String toString() {
return "This is car";
}
}
class SportCar extends Car {
protected int speedMax = 300;
public int getSpeedMax() {
return speedMax;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment