Let's say we have a few test interfaces/classes like this:
abstract class Plant {
public abstract String getName();
}
interface Eatable { }
class Apple extends Plant implements Eatable {
@Override
public String getName() {
return "Apple";
}
}
class Rose extends Plant {
@Override
public String getName() {
return "Rose";
}
}
interface Animal {
<T extends Plant & Eatable> void eat(T plant);
}
You can see Animal.eat
is a generic method with constraints. Now I have my Human
class like this:
class Human implements Animal {
@Override
public void eat(Plant plant) {
}
}
which compiles fine. You can see Human.eat
is less constrained than Animal.eat
because the Eatable
interface is lost.
Q1: Why doesn't the compiler complain about this inconsistency?
Q2: If Plant&Eatable
downgrades to Plant
is acceptable for the compiler, why it complains on eat(Object plant)
?
javac
on the command line, and it doesn't get an error. But if I changePlant
toRose
in the parameter list ofHuman.eat
, I now get an error. – EncomiumEatable
method on an object of a class that doesn't implementEatable
(assuming we added some methods toEatable
). – Encomium