Subtyping
Subtyping is transitive, meaning that if one type is a subtype of a second, and the second is a subtype of a third, then the first is a subtype of the third.If one type is a subtype of another, we also say that the second is a supertype of the first.We also say, trivially, that every type is a subtype of itself.
public class Subtyping {
public static void main(String[] args) {
Crow crow = new Crow();
crow.shout();
crow.fly();
crow.sleep();
}
}
class Animal {
public void sleep() {
System.out.println("I am sleeping");
}
}
class Bird extends Animal{
public void fly() {
System.out.println("I am flying");
}
}
class Crow extends Bird {
public void shout() {
System.out.println("I am crow");
}
}
</br>
Substitution Principle
a variable of a given type may be assigned a value of any subtype of that type, and a method with a parameter of a given type may be invoked with an argument of any subtype of that type.
interface Collection<E> {
public boolean add(E elt);
...
}
List<Number> nums = new ArrayList<Number>();
nums.add(2);
nums.add(3.14);
assert nums.toString().equals("[2, 3.14]");
Here, subtyping is used in two ways for each method call. The first call is permitted because <strong>nums has type List<Number>, which is a subtype of Collection<Number>, and 2 has type Integer (thanks to boxing), which is a subtype of Number</strong>. The second call is similarly permitted. In both calls, the E in List<E> is taken to be Number.
网友评论