Subtyping and the Substitution Principle

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");
    }
}



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 {
    public boolean add(E elt);
    ...
}

List nums = new ArrayList();
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 nums has type List, which is a subtype of Collection, and 2 has type Integer (thanks to boxing), which is a subtype of Number. The second call is similarly permitted. In both calls, the E in List is taken to be Number.

你可能感兴趣的:(Subtyping and the Substitution Principle)