Abstract


  • A data type is a subtype of a data type if a piece of code written for a variable of data type can also be safely used on a variable of data type
  • The relationship can be represented with , where is also known as the super type of

Java Primitive Subtyping

Subtyping among Java Primitive Datatype

  • byte <: short <: int <: long <: float <: double
  • char <: int <: long <: float <: double

Important

Subtyping is Reflexive,

Subtyping is Transitive, if and , then

OOP

It can be a type that inherits from a OOP Class or Interface. Less specific compared to Sub-class

Compile-time Type vs Run-time Type

  • We can assign a subtype instance to a supertype variable during compilation
  • At runtime, we can call all methods with matching signatures declared in the supertype on the subtype instance, including those overridden in the subtype

Important

The runtime type of an object is always a subtype of its compile-time type.

At compile time, the compile-time type is identified, and the method descriptor in the compile-time type is stored in the generated bytecode. When the runtime type is determined during runtime, Java then looks for an accessible method with a matching descriptor in that runtime type.

Check if the run-time type of obj is a subtype of Type in Java

obj instanceof Type