What is the purpose of generics in OOP, and how do they improve type safety without sacrificing reusability?

Last updated 5 July 2026

  • Generics parameterize classes and methods by type, so one definition (e.g. Stack<T>) works for many types.

  • The compiler checks type arguments like List<String>, removing manual casts and runtime ClassCastException risks from coding against Object.

  • Bounded parameters (e.g. T extends Comparable) constrain types while staying flexible. Java uses type erasure, while C#/C++ reify or specialize per type.

  • https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/
All Object-Oriented Programming interview questions