Chapter 18

Generics

Yet another form of polymorphism is provided by the facility known as a generic (in C++, a template). Generics provide a way of parameterizing a class or a function by use of a type, just as normal parameters to a function provide a way to define an algorithm without identifying specific values.

With generics, a name is defined as a type parameter. This parameter can then be used within the class definition just as if it were a type, although no properties of the type are known when the class description is being read by the compiler. At some later point the type parameter is matched with a specific type and a complete declaration can be formed.

Among object-oriented languages generics are best known in C++, Beta, and Eiffel. ...

Get An Introduction to Object-Oriented Programming, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.