Implementations
Introduction / Definition
Polymorphic structures can be implemented using generics, templates, interfaces, inheritance, or dynamic typing. The implementation should preserve the structure's interface and invariants. Type parameters separate element type from structural logic. The language determines how type information is represented at runtime.
Syntax / Structure / Formula
Generic interface → type parameter → concrete instantiation
Detailed Explanation
A generic node Node<T> stores T data and a reference to another Node<T>. The same implementation can support many types.
Example
Example: Java: class Node<T>\{ T data; Node<T> next; \}.
Output / Result
Node<Integer>, Node<String>, and Node<MyClass> can reuse the same structure.
Important Points / Notes
- Preserve invariants for every T.
- Avoid unnecessary casts.
- Consider runtime representation where relevant.
Mini Practice Question
How does Node<T> improve reuse?