An introduction to Object-oriented language (IV.)

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.