Introduction to Subtypes


Types and Subtypes

What is a Subtype

Declaring Subtypes in Ada

Why Do We Use Subtype Natural?

Type Checking and Subtypes

Subtypes where Parent Types are Expected

Parent Types where Subtypes are Expected

  • Compiler must generate runtime check for negative value passed to sum_1_to_n

  • Subtypes Example: Type Checking, Constraint Error

    Subtypes: Implicit Conversion

    Subtypes and Strong Typing

    1. Strong type checking at compile time: Compiler verifies that operations operate on valid variables
    2.         myFloat: float := 3;   -- invalid
              myVar := 2 + 3.0;      -- invalid
    3. Compiler generates runtime checks to verify variables are assigned only valid values
    4.             get(myNat);        -- needs runtime check
                  myPos := myNat;    -- needs runtime check

    Subtypes and Java

    Numeric Subtypes and Java

    Class Subtypes and Java