0% found this document useful (0 votes)
9 views

Generics in Java KT Presentation

Uploaded by

Arpan Panigrahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Generics in Java KT Presentation

Uploaded by

Arpan Panigrahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Generics in

Java
PRESENTER: ARPAN PANIGRAHI
DATE: [TODAY'S DATE]
Introduction

 Welcome, everyone. Today, we will be


discussing Generics in Java. Generics were
introduced in JDK 5 to provide stronger type
checks at compile time and to support generic
programming. By the end of this session, you
will understand what generics are, why we use
them, and how to implement them effectively in
your Java programs.
What Are Generics?

 Generics allow types (classes and methods) to


operate on objects of various types while
providing compile-time type safety. They enable
you to write more flexible and reusable code.
Why Use Generics?

 Type Safety
 Elimination of Type Casting
 Code Reusability
Basic Concepts

 Type Parameters: <T>, <E>, <K, V>, etc.


Generic Syntax:
class Box<T> {
private T t;
public void set(T t) { this.t = t; }
public T get() { return t; }
}
Advantages

 Type Safety: Prevents ClassCastException at


runtime.
 Elimination of Type Casting: Avoids explicit
casting.
 Code Reusability: Enables the same code to
work with different data types.
Generic Classes and
Interfaces
 Generic Class:
public class Container<T> {
private T item;
public void setItem(T item) { this.item = item; }
public T getItem() { return item; }
}

 Generic Interface:
public interface Pair<K, V> {
public K getKey();
public V getValue();
}
Generic Methods

 Generic Method:
public <T> void printArray(T[] array) {
for (T element : array) {
System.out.println(element);
}
}
Bounded Type
Parameters
 Upper Bound:
public <T extends Number> void printNumber(T
number) {
System.out.println(number);
}

 Lower Bound:
public void addNumbers(List<? super Integer> list)
{
list.add(new Integer(10));
}
Wildcards in Generics

 Unbounded Wildcard:
List<?> list = new ArrayList<String>();

 Upper Bounded Wildcard:


public void process(List<? extends Number> list) {
// ...
}

 Lower Bounded Wildcard:


public void addIntegers(List<? super Integer> list) {
// ...
}
Common Pitfalls and
Limitations
 Type Erasure: Generic type information is
erased at runtime.
 Generic Arrays: Cannot create arrays of
parameterized types.
Practical Examples

 Collections Framework:
List<String> list = new ArrayList<>();
list.add("Hello");

 Custom Generic Class:


public class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() { return key; }
public V getValue() { return value; }
}
Conclusion

 Summary of Key Points:


- What are Generics?
- Why use Generics?
- Generic Classes, Interfaces, and Methods
- Bounded Type Parameters and Wildcards
- Practical Examples
Q&A Session

 Questions?

You might also like