Browse OOP reference
Types and contracts

What is nominal typing?

On this page

Nominal typing recognises declared type identities and relationships. In Java two interfaces can declare the same method, yet a class implementing only one need not be assignable to the other. Distinct nominal types can prevent values such as CustomerId and InvoiceId from being accidentally interchanged.

Worked example

Rust
struct InvoiceId(u64);
struct CustomerId(u64);
fn invoice_number(id: InvoiceId) -> u64 { id.0 }
fn main() {
    let customer = CustomerId(7);
    assert_eq!(invoice_number(InvoiceId(7)), customer.0);
    // invoice_number(customer) would fail type checking.
}

The Rust Programming Language on structs

Explore class relationships in the OOP Playground

What is method overloading?What is a method?What is a class?

All OOP questions

Search ObjectFAQ