Lab Manual - Lab 19-Generics
Lab Manual - Lab 19-Generics
Objective:
● To understand Generics
● Code Reusability: Generics allow you to create a method, class, or interface once and
reuse it with different data types.
● Type Safety: By using generics, type-related errors are caught at compile time, making
the code more robust and secure.
● Elimination of Type Casting: Generics handle type conversion automatically, removing
the need for manual type casting when accessing data.
● Improved Readability and Maintainability: Specifying types enhances code clarity
and simplifies future modifications.
Task:
1. Generic Class:
Create a generic class Storage<T> with a private attribute value, methods to set and
get its value, and test it in a main method.
2. Generic Method:
Create a class Utility with a static generic method swap() that swaps two values of the
same type and prints the values before and after the swap. Test it in a main method.
//1. Generic Class
class Storage<T> {
private T value;
public T getValue() {
return value;
}
}