Abstract


  • Also known as Type Conversion
  • The process of converting a variable from one data type to another. This allows the variable to be treated as a different data type and utilise its properties. Type conversion can be implicit, where the conversion is handled automatically by the programming language, or explicit, where the conversion is performed manually by the programmer.

Explicit type casting is dangerous

Data Loss: Converting from a floating-point number (float) to an integer (int) results in the truncation (loss) of the decimal portion (narrowing type conversion). This isn’t allowed in in Java, but allowed in C, thus Java is more strongly typed.

Loss of Object State or Methods: Widening type conversions can lead to the loss of access to specific subclass attributes and methods.

Invalid Casting: Incorrectly casting objects (e.g., trying to treat a Cat object as if it were a Dog object) will typically result in a runtime error (such as a ClassCastException in Java).

Important

Type casting changes Compile-time Type vs Run-time Type.

Widening Type Conversion

  • Casting a subclass object to its superclass
  • A value of datatype can be assigned to a variable of type if and only if is a subtype of
  • Supported by Java implicitly

Narrowing Type Conversion

Explicit narrowing type conversion in Java

It tells compiler we know what we are doing, don’t enforce any checks, thus we will get runtime error if the type conversion is not compatible.

Why do we need narrowing?

Dynamic binding allows us to pass subtypes to a supertype. However, if we want to use methods specific to the subtype, we need to perform narrowing. Otherwise, we’ll encounter compilation errors. This is because, to the compiler, the compile-time type (the supertype) doesn’t support those subtype-specific methods.