Four, complete separation (continue to separate subclassing and subtyping)
In the second section, we discuss the partial separation of subclassing and subtyping methods, namely subclassing-implies-subtyping. Many of today's object-oriented languages, such as Java and C #, use this technique. In addition, there is a way to further isolate subclassing and subtyping. This method, called inheritance-is-not-subtyping, facilitates the reuse of code to a greater extent by completely separating the connections between subclassing and subtyping.
It is largely due to the fact that people want to use the self type (such as parameters of the self type) at the counter covariant position. Of course, the cost of increasing the ability to inherit is that the subsumption is less flexible. When the self type appears in the reverse covariance position, subclass no longer means subtype, so subsumption does not exist.
Consider the following two types:
ObjectType Max is
var n: Integer;
method max(other:Max): Max;
end;
ObjectType MinMax is
var n: Integer;
method max(other:MinMax): MinMax;
method min(other:MinMax): MinMax;
end;
Consider two more classes:
class MaxClass is
var n:Integer :=0;
method max(other: Self): Self is
if self.n > other.n then return self else return other end;
end;
end;
subclass MinMaxClass of MaxClass is
method min(other: Self): Self is
if self.n < other.n then return self else return other end;
end;
end;