3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Create generic classes in Java.
- Implement generic methods.
- Apply type-safe collections.
- Use upper-bound and lower-bound wildcards.
- Develop reusable Java components using generics.
3.2 Overview
Generics allow Java classes, interfaces, and methods to work with different data types while maintaining type safety. Generics improve:
- code reusability,
- flexibility,
- readability,
- and compile-time error checking.
Generics are important in:
- collection frameworks,
- enterprise software,
- reusable libraries,
- and scalable Java applications.
This lesson introduces learners to practical generic programming concepts in Java.
Understanding generics is important because they reduce runtime errors and improve software design.
PA0201 — Implement Generics for Custom List
Generic classes can store different data types safely.
Java Example:
import java.util.*;
class CustomList {
private List items = new ArrayList<>();
public void addItem(T item) {
items.add(item);
}
public T getItem(int index) {
return items.get(index);
}
public void displayItems() {
for (T item : items) {
System.out.println(item);
}
}
}
public class GenericExample {
public static void main(String[] args) {
CustomList studentNames = new CustomList<>();
studentNames.addItem(“Alice”);
studentNames.addItem(“Bob”);
CustomList marks = new CustomList<>();
marks.addItem(85);
marks.addItem(92);
studentNames.displayItems();
marks.displayItems();
}
}
Practical Activity
Learners must:
- create generic classes,
- store different data types,
- and display collection data safely.
PA0202 — Extend Custom List with a Generic Return Method
Generic methods return flexible data types.
Java Example:
class ExtendedList extends CustomList {
public T getFirst(List list) {
return (list.isEmpty()) ? null : list.get(0);
}
}
Practical Activity
Learners must:
- extend generic classes,
- create generic return methods,
- and retrieve collection data.
PA0203 — Use Wildcards (Upper Bound and Lower Bound)
Wildcards support flexible type handling in Java generics.
Java Example:
import java.util.*;
public class WildcardExample {
public static double sumOfMarks(List<? extends Number> list) {
double sum = 0.0;
for (Number n : list) {
sum += n.doubleValue();
}
return sum;
}
public static void addIntegers(List<? super Integer> list) {
list.add(50);
list.add(75);
}
}
Practical Activity
Learners must:
- apply upper-bound wildcards,
- apply lower-bound wildcards,
- and process flexible data types.
AK0201 — Functions and Uses of Generics in Java Programming
Generic Feature — Function
Type Safety — Prevents runtime type errors
Code Reusability — Reuses classes and methods
Flexibility — Supports multiple data types
Wildcards — Supports flexible generic operations
Practical Activity
Learners must:
- identify generic benefits,
- explain wildcard functionality,
- and apply reusable programming structures.
IAC0201 — Expected Results with Generics in Java are Achieved
Expected Results
- Generic classes are implemented correctly.
- Generic methods return valid data types.
- Wildcards function correctly.
- Code compiles without type errors.
- Generic structures improve software flexibility.
3.5 Key Notes / Summary
- Generics improve type safety.
- Generic classes support reusable code.
- Generic methods support flexible return types.
- Wildcards improve generic flexibility.
- Upper bounds use ? extends.
- Lower bounds use ? super.
- Generics reduce runtime errors.