An introduction to Object-oriented language (II.)

Source: Internet
Author: User
Tags inheritance

4. Subsumption and Dynamic Dispatch (Translator press: Hehe, the donkey, can't find suitable translation)

From the above examples, it seems that subclasses are just used to borrow some definitions from the parent class to avoid duplication. But when we think about subsumption, things are a little different. What is Subsumption? Take a look at the following example:

var myCell: InstanceTypeOf(cell) := new cell;

var myReCell: InstanceTypeOf(reCell) := new reCell;

procedure f(x: InstanceTypeOf(cell)) is … end;

Look at the following code:

myCell := myReCell;

f(myReCell);

In both lines of code, the first line assigns a variable of a instancetypeof (ReCell) type to a instancetypeof (cell) variable. The second line passes a variable of type instancetypeof (ReCell) as a parameter to a function with a parameter type of instancetypeof (cell).

This usage is illegal in a Pascal-like language. In an object-oriented language, it is the correct usage, based on the following rules. This rule is often called subtype polimorphism, which is subtype polymorphism (translator: In fact, subtyping should be the most different from OO language in other languages)

If C ' is a subclass of C, and O ' is an instance of C ', then O ' is also an instance of C.

More strictly speaking:

If C ' is a subclass of C and O ': instancetypeof (c '), then O ': instancetypeof (c).

With a careful analysis of the above rule, we can introduce a subtype relationship that satisfies reflexivity and transitivity between the types of instancetypeof, and we use the symbols to represent them. (Translator: Reflexive that is, for any A, a relationship A is established, for example, in mathematics, the equivalence relationship is reflexive.) And transitivity is that if a relationship B, b relationship C, you can launch a relationship c. Greater than, less than, etc relationships are transitive.

So the above rule can be broken down into two rules:

1. For any a:a, if A;: B, then a:b.

2. Instancetypeof (c '): Instancetypeof (c) When and only if C ' is a subclass of C '

The first rule is called subsumption. It is the only criterion for determining subtype (note, is subtype, not subclass).

The second rule can be called subclassing-is-subtyping (subclasses are subtypes, Raozui?). )

In general, inheritance is related to subclassing, so this rule can also be called: inheritance-is-subtyping (Inheritance is subtype)

All object-oriented languages support subsumption (it can be said that without subsumption, it is not object-oriented).

Most class-oriented object-oriented languages do not distinguish between subclassing and subtyping. However, some of the newest object-oriented languages take the approach of separating subtyping from subclassing. That is, a is a subclass of B, but an object of Class A cannot be used as an object of Class B. (translator: Somewhat like the private inheritance in C + +, but the content is richer than it)

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.