Information about Subtype

In computer science, a subtype is a datatype that is generally related to another datatype (the supertype) by some notion of substitutability, meaning that computer programs written to operate on elements of the supertype can also operate on elements of the subtype. More specifically, the supertype-subtype relation is often taken to be the one defined by the Liskov substitution principle; however, any given programming language may define its own notion of subtyping, or none at all.

Example

Enlarge picture
Example of Subtypes: Birds
A simple practical example of subtypes is shown in the diagram, right. The generic object "bird" (or, supertype) gives rise to three derived objects (or, subtypes) "duck", "cuckoo" and "ostrich". Each of these varies the basic "bird" concept in its own way, but still inherits many "bird" characteristics. A data object may be declared as any one of the four types. The UML notation is used in this diagram, with open-headed arrows showing the direction and type of the relationship between the supertype and its subtypes.

In most class-based object-oriented languages, subclasses give rise to subtypes: if A is a subclass of B, then an instance of class A may be used in any context where an instance of type B is expected; thus we say A is a subtype of B. A consequence of this is that any variable or parameter declared as having type B might, at run time, hold a value of class A; in this situation many object-oriented programmers would say that B is the variable's static type and A is its dynamic type. Exceptions to this rule include private inheritance in C++ (which does not create subtypes), and certain operations on derived types in Eiffel, wherein features inherited from a base class can be removed or modified in a way which violates the subtyping rule.

As another example, a language might allow integer values to be used wherever floating-point values are expected, or it might define a type number containing both the integers and the reals. In the first instance, the integer type would be a subtype of the floating-point type; in the second, those two types might be unrelated to each other by subtyping but both would be subtypes of number.

Programmers may take advantage of subtyping to write code in a more abstract manner than would be possible without it. Consider the following example:

function max (x as number, y as number) is if x < y then return y else return x end

If integer and real are both subtypes of number, then values of either type can be passed to this function. Because of this, subtyping is often considered a form of polymorphism. The above example can also be compared to the template feature of C++.

In type theory, the subtype relation is often written <:, so that A<:B means that A is a subtype of B. Subtyping in type theory is characterized by the fact that any expression of type A may also be given type B if A<:B; the formal typing rule that codifies this is known as the subsumption rule.

Subtyping schemes

Type theorists make a distinction between nominal subtyping, in which only types declared in a certain way may be subtypes of each other, and structural subtyping, in which the structure of two types determines whether or not one is a subtype of the other. The class-based object-oriented subtyping described above is nominal; a structural subtyping rule for an object-oriented language might say that if objects of type A can handle all of the messages that objects of type B can handle (that is, if they define all the same methods), then A is a subtype of B regardless of whether either inherits from the other. Sound structural subtyping rules for types other than object types are also well known.

Implementations of programming languages with subtyping fall into two general classes: inclusive implementations, in which the representation of any value of type A also represents the same value at type B if A<:B, and coercive implementations, in which a value of type A can be automatically converted into one of type B. The subtyping induced by subclassing in an object-oriented language is usually inclusive; subtyping relations that relate integers and floating-point numbers, which are represented differently, are usually coercive.

In almost all type systems that define a subtyping relation, it is reflexive (meaning A<:A for any type A) and transitive (meaning that if A<:B and B<:C then A<:C). This makes it a preorder on types.

See also

External links

Computer science, or computing science, is the study of the theoretical foundations of information and computation and their implementation and application in computer systems.
..... Click the link for more information.
In programming languages a data type defines a set of values and the allowable operations on those values[1]. For example, in the Java programming language, the "int" type represents the set of 32-bit integers ranging in value from -2,147,483,648 to 2,147,483,647, and
..... Click the link for more information.
Substitutability is a principle in computer programming. It states that, if S is a subtype of T, then objects of type T in a computer program may be replaced with objects of type S (i.e.
..... Click the link for more information.
A computer program is one or more instructions that are intended for execution by a computer. Specifically, it is a symbol or combination of symbols forming an algorithm that may or may not terminate, and that algorithm is written in a programming language.
..... Click the link for more information.
In object-oriented programming, the Liskov substitution principle is a particular definition of subtype that was introduced by Barbara Liskov and Jeannette Wing in a 1993 paper entitled Family Values: A Behavioral Notion of Subtyping [1].
..... Click the link for more information.
A programming language is an artificial language that can be used to control the behavior of a machine, particularly a computer. Programming languages, like natural languagess, are defined by syntactic and semantic rules which describe their structure and meaning respectively.
..... Click the link for more information.
In the field of software engineering, the Unified Modeling Language (UML) is a standardized specification language for object modeling. UML is a general-purpose modeling language that includes a graphical notation used to create an abstract model of a system, referred to as a
..... Click the link for more information.
An object-oriented programming language (also called an OO language) is one that allows or encourages, to some degree, object-oriented programming techniques such as encapsulation, inheritance, interfaces, and polymorphism.
..... Click the link for more information.
In object-oriented programming, a subclass is a class that inherits some properties from its superclass.

You can usually think of the subclass as being "a kind of" its superclass, as in a "a Manx is a kind of cat", or "a square is a kind of rectangle":
  • A cat

..... Click the link for more information.
C++
Paradigm: Multi-paradigm
Appeared in: 1983
Designed by: Bjarne Stroustrup
Typing discipline: Static, unsafe, nominative
Major implementations: G++, Microsoft Visual C++, Borland C++ Builder
Dialects: ISO/IEC C++ 1998, ISO/IEC C++ 2003
..... Click the link for more information.
Eiffel
Paradigm: object-oriented
Appeared in: 1986
Designed by: Bertrand Meyer
Developer: Bertrand Meyer & Eiffel Software
Typing discipline: static typing, strong typing
Major implementations: EiffelStudio, SmartEiffel, Visual Eiffel
..... Click the link for more information.
This article or section may be confusing or unclear for some readers.
Please [improve the article] or discuss this issue on the talk page. This article has been tagged since April 2007.
..... Click the link for more information.
In computer programming, templates are a feature of the C++ programming language that allow code to be written without consideration of the data type with which it will eventually be used. Templates support generic programming in C++.
..... Click the link for more information.
In mathematics, logic and computer science, type theory is any of several formal systems that can serve as alternatives to naive set theory, or the study of such formalisms in general.
..... Click the link for more information.
In logic, a rule of inference is a function from sets of formulae to formulae. The argument is called the premise set (or simply premises) and the value the conclusion.
..... Click the link for more information.
In computer science nominative type system is a major class of type system, in which type compatibility and equivalence is determined by explicit declarations and/or the name of the types.
..... Click the link for more information.
A structural type system is a major class of type system, in which type compatibility and equivalence are determined by the type's structure, and not through explicit declarations.
..... Click the link for more information.
confusing or unclear for some readers.

Please help [ improve the article] or discuss these issues on the talk page. In object-oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods
..... Click the link for more information.
In object-oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The new classes, known as derived classes, take over (or inherit
..... Click the link for more information.
In mathematics, especially in order theory, preorders are binary relations that satisfy certain conditions. For example, all partial orders and equivalence relations are preorders. The name quasiorder is also common for preorders.
..... Click the link for more information.


A derived type is a type given a new type but structurally the same as the original type.
..... Click the link for more information.
Type may refer to:

In computing:
  • Data type, collection of values used for computations
  • Type system, defines a programming language's response to data types
  • Type theory, basis for the study of type systems
In mathematics:

..... Click the link for more information.
A covariant type operator in a type system preserves the ordering ≤ of types. A contravariant operator reverses ≤. If neither of these apply, the operator is invariant. These terms come from category theory.
..... Click the link for more information.
The Circle-ellipse problem in software development (sometimes known as the square-rectangle problem) illustrates a number of pitfalls which can arise when using subtype polymorphism in object modelling.
..... Click the link for more information.
Class-based programming, or more commonly class-orientation, refers to the style of object-oriented programming in which inheritance is achieved by defining classes of objects, as opposed to the objects themselves (compare Prototype-based programming).
..... Click the link for more information.
Subclass may refer to:
  • Subclass (biology), a taxonomic rank intermediate between class and superorder
  • Subclass (computer science), a class that is derived from another class or classes

..... Click the link for more information.
Bertrand Meyer (born 1950 in France) developed the Eiffel programming language, and is an author, academic and consultant in the field of computer languages.

Meyer pursues the ideal of simple, elegant and user-friendly computer languages and is one of the earliest and most
..... Click the link for more information.


This article is copied from an article on Wikipedia.org - the free encyclopedia created and edited by online user community. The text was not checked or edited by anyone on our staff. Although the vast majority of the wikipedia encyclopedia articles provide accurate and timely information please do not assume the accuracy of any particular article. This article is distributed under the terms of GNU Free Documentation License.
Herod_Archelaus


page counter