What are the differences?
A subtype is a type together with a constraint; a value is said to belong to a subtype of a given type if it belongs to the type and satisfies the constraint; the given type is called the base type of the subtype.
The %TYPE attribute lets you declare a constant, variable, field, or parameter to be of the same data type a previously declared variable, field, record, nested table, or database column. For the other hand, a SUBTYPE does not introduce a new type; rather, it places an optional constraint on its base type.
A derived type is formed by using one or more basic types in combination. Using derived types, an infinite variety of new types can be formed. The array and structure types are collectively called the aggregate types. Note that the aggregate types do not include union types, but a union may contain an aggregate member.
As for Ada, records and arrays would be language-supported data structures. Packages are also a kind of a data structure.
First of all, terminology: it's "Ada", not "ADA" -- it's named after "Ada Lovelace"; it is not an acronym.
A subtype is compatible with its base type, so you can mix operands of the base type with operands of the base type. For example:
subtype Week_Days is Integer range 1..7;
Since this is a subtype, you can (for example) add 1
to a weekday to get the next weekday.
A derived type is a completely separate type that has the same characteristics as its base type. You cannot mix operands of a derived type with operands of the base type. If, for example, you used:
type Week_Day is new Integer range 1..7;
Then you would not be able to add an integer to a weekday to get another weekday. To do manipulations on a derived type, you'd normally define those manipulations yourself (e.g., create a package). At the same time, a derived type does "inherit" all the operations of its base type (even some that may not make sense) so you do still get addition.
From Wikibooks:
Subtypes of a given type will be compatible with each other.
A derived type is a new, full-blown type created from an existing one. Like any other type, it is incompatible with its parent; however, it inherits the primitive operations defined for the parent type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With