In what ways are subtypes different from subclasses in usage?

后端 未结 6 1428
难免孤独
难免孤独 2020-12-08 05:40

A subtype is established when a class is linked by means of extending or implementing. Subtypes are also used for generics.

How can I differentiate subtyping from su

6条回答
  •  时光取名叫无心
    2020-12-08 06:03

    For once, Wikipedia gives a very straight answer to the question:

    http://en.wikipedia.org/wiki/Subtype_polymorphism

    Subtyping should not be confused with the notion of (class or object) inheritance from object-oriented languages; subtyping is a relation between types (interfaces in object-oriented parlance) whereas inheritance is a relation between implementations stemming from a language feature that allows new objects to be created from existing ones. In a number of object-oriented languages, subtyping is called interface inheritance.

    In short, subtyping occurs when you derive an interface (method signatures/access points/ways of reacting to the outside world) from another whereas subclassing occurs when you derive an implementation (methods, attributes/internal state and inside logic) of a class from another class through inheritance.

    This terminology is not often used in this way and type usually refers to the data type.

提交回复
热议问题