What is Structural Typing?
Learn structural typing in OOP — shape-based compatibility vs nominal typing — with examples and common interview questions.
Expected Interview Answer
Structural typing is a type-compatibility rule where two types are considered compatible if they expose the same shape — the same members with compatible signatures — regardless of whether they declare any explicit relationship.
Under structural typing, the compiler or runtime compares the members a type exposes rather than checking a declared hierarchy. TypeScript’s interfaces and Go’s interfaces are structurally typed: any object or struct that happens to have the required methods or fields satisfies the type, with no implements clause needed. This gives strong flexibility for integrating unrelated code and enables patterns like duck typing at compile time, but it can allow accidental compatibility between types that only coincidentally share a shape, which nominal typing would have blocked. It is the direct counterpart to nominal typing.
- No boilerplate implements declarations required
- Enables flexible integration of unrelated types
- Encourages small, focused interfaces (shape-based contracts)
- Compile-time safety without upfront hierarchy planning
AI Mentor Explanation
A scout doesn’t care which academy a player’s certificate came from — if the player can bat, bowl and field to the required standard right now, they are treated as match-ready, regardless of any formal registration. Two players from entirely different backgrounds both qualify purely because their observable skills match what’s needed. Structural typing works the same way: a type is compatible if its members match the required shape, with no declared relationship needed.
Step-by-Step Explanation
Step 1
Define the required shape
A type (interface) specifies the members it expects — names and signatures, nothing about lineage.
Step 2
Compare shapes, not declarations
The compiler checks whether a candidate type has matching members, ignoring any implements clause.
Step 3
No explicit relationship needed
Any object with the right shape satisfies the type, even from an entirely unrelated hierarchy.
Step 4
Contrast with nominal typing
A nominal system (Java classes) would reject the same object unless it explicitly declared the relationship.
What Interviewer Expects
- A clear definition contrasting structural vs nominal typing
- A concrete example (TypeScript or Go interfaces)
- Understanding that no explicit implements/extends is required
- Awareness of the accidental-compatibility risk trade-off
Common Mistakes
- Confusing structural typing with dynamic/loose typing
- Assuming Java interfaces are structurally typed (they still require implements)
- Not mentioning any real language example
- Overstating structural typing as always safer than nominal typing
Best Answer (HR Friendly)
“Structural typing means a type is considered compatible with another purely because it has the same shape — the same fields and methods — even if it never explicitly says it’s related. TypeScript and Go work this way: if an object has the right members, it satisfies the interface automatically. It’s more flexible than Java’s approach, but it can occasionally let unrelated types match by coincidence.”
Code Example
// Java is nominally typed, so this illustrates the CONTRAST:
// even with identical method shape, no declared relation means no compatibility.
interface Flyable {
void fly();
}
class Bird implements Flyable {
public void fly() { System.out.println("Bird flying"); }
}
// Structurally identical, but Java rejects this without "implements Flyable"
class Drone {
public void fly() { System.out.println("Drone flying"); }
}
// In a structurally typed language (e.g. TypeScript/Go), Drone would
// satisfy Flyable automatically because its shape matches -- Java requires
// an explicit "implements Flyable" clause instead.
Follow-up Questions
- Which mainstream languages use structural typing by default?
- How does structural typing relate to duck typing?
- What risk does structural typing introduce that nominal typing avoids?
- How would you emulate structural typing in a nominally typed language like Java?
MCQ Practice
1. Structural typing determines compatibility based on?
Structural typing compares the shape (members and signatures) a type exposes, not any declared relationship.
2. Which language is a well-known example of structural typing?
TypeScript interfaces are satisfied by any object with a matching shape, with no explicit 'implements’ required.
3. A risk unique to structural typing is?
Because only shape is checked, two conceptually unrelated types can accidentally satisfy the same interface.
Flash Cards
Structural typing in one line? — Type compatibility is based purely on matching shape (members), not declared relationships.
Example languages? — TypeScript and Go use structural interface typing.
Opposite of structural typing? — Nominal typing, which requires explicit declared relationships.
Main risk? — Accidental compatibility between unrelated types that coincidentally share a shape.