Generics

What Are Generics?

Generics are a way of generalizing types; they define the data type at run time. Generics are called parametric polymorphism in type theory. ‘Poly’ is multiple, ‘morph’ is form over a given parameter (‘parametric’) meaning multiple forms of a given parameter.

They can be applied to methods, functions, structures, enumerations, collections, and traits. This helps to reuse the same code but with a different type.

Syntax

The <T> is known as the type parameter and is used to declare a generic construct. T can be any data-type.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy