codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Generic Programming (Templates & Generics)

Updated 2026-04-28
1 min read

Generic Programming (Templates & Generics)

Generic Programming allows you to write a single class or function that works with multiple data types, determined at compile time. Instead of writing separate IntList, StringList, and DoubleList classes, you write one List<T> that works with any type T.

1. Java Generics

// Without Generics (uses raw Object — unsafe)
List list = new ArrayList();
list.add("hello");
String s = (String) list.get(0); // Manual cast — can throw ClassCastException at runtime

// With Generics (type-safe)
List<String> list = new ArrayList<>();
list.add("hello");
String s = list.get(0); // No cast needed. Compiler enforces type safety.
list.add(42); // COMPILE ERROR — caught before the program runs

Generic Classes:

class Pair<T, U> {
    T first;
    U second;
    Pair(T first, U second) { this.first = first; this.second = second; }
}
Pair<String, Integer> p = new Pair<>("Alice", 30);

Generic Methods:

static <T extends Comparable<T>> T findMax(T a, T b) {
    return a.compareTo(b) > 0 ? a : b;
}

Bounded Type Parameters:

<T extends Number> restricts T to Number or its subclasses. This allows calling Number methods like intValue() on T.

2. C++ Templates

C++ uses Templates instead of Generics. Templates are resolved at compile time by generating separate code for each type used (monomorphization).

template <typename T>
T findMax(T a, T b) {
    return (a > b) ? a : b;
}

findMax(10, 20);       // Generates findMax<int>
findMax(3.14, 2.71);   // Generates findMax<double>

3. Java Type Erasure

Java Generics use Type Erasure: generic type information is removed at compile time and replaced with Object (or the bound type). This means you cannot use new T(), instanceof T, or create generic arrays at runtime.

4. Benefits

  • Type Safety: Errors caught at compile time, not runtime.
  • Code Reuse: One implementation works for all types.
  • Elimination of Casts: No manual casting needed.


PreviousInterfaces & Abstract ClassesNextException Handling in OOP

Recommended Gear

Interfaces & Abstract ClassesException Handling in OOP