Fix that Java: Mugs are not the Same
Ahh, either mugs or code but one of them is definitely broken…. or both.
public class Main {
public static void main(String[] args) {
Mug classic = new Mug(1.2);
PlasticMug plastic = new PlasticMug(1.2, "polycarbonat");
System.out.println(plastic.equals(classic));
}
}
public class Mug {
private double capacity;
public Mug(double capacity) { this.capacity = capacity; }
public double getCapacity() { return capacity; }
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (!(o instanceof Mug)) {
return false;
}
Mug other = (Mug) o;
return other.capacity == this.capacity;
}
}
public class PlasticMug extends Mug {
private String plastic;
public PlasticMug(double capacity, String plastic) {
super(capacity);
this.plastic = plastic;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (!(o instanceof PlasticMug)) {
return false;
}
PlasticMug other = (PlasticMug) o;
return other.getCapacity() == this.getCapacity() && other.plastic.equals(plastic);
}
}
Try to fix … one or both of them.