Generics What is Generics?



Download 1.18 Mb.
Page1/15
Date20.02.2022
Size1.18 Mb.
#58285
  1   2   3   4   5   6   7   8   9   ...   15
Generics-new

Generics

What is Generics?

Generics is the capability to parameterize types. With this capability, you can define a class or a method with generic types that can be substituted using concrete types by the compiler. For example, you may define a generic stack class that stores the elements of a generic type. From this generic class, you may create a stack object for holding strings and a stack object for holding numbers. Here, strings and numbers are concrete types that replace the generic type.

Why Generics?

The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method may work with. If you attempt to use the class or method with an incompatible object, a compile error occurs.

Generic Type


Generic Instantiation

Runtime error

Compile error

Improves reliability



Download 1.18 Mb.

Share with your friends:
  1   2   3   4   5   6   7   8   9   ...   15




The database is protected by copyright ©ua.originaldll.com 2024
send message

    Main page