What is the difference between Type and Class?

前端 未结 20 2306
梦谈多话
梦谈多话 2020-11-28 01:00

What makes a type different from class and vice versa?

(In the general language-agnostic sense)

20条回答
  •  清酒与你
    2020-11-28 01:09

    Different classes may describe the same type.

    Type consists of these parts:

    1. Operations = syntax
    2. Description of operations = semantics

    Class consists of these parts:

    1. Operations = syntax
    2. Implementation (= various implementations describe same semantics)

    Some notes:

    • Interface (as in Java) is not type, because it does not describe semantics (describes only syntax)

    • Subclass is not subtype, because subclass may change semantics defined in superclass, subtype cannot change supertype semantics (see Liskov Substitution Principle, e.g. this LSP example).

提交回复
热议问题