Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Generics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Main {
public static void main(String[] args) {

// initialize generic class
// with Integer data
GenericsClass<Integer> intObj = new GenericsClass<>(5);
System.out.println("Generic Class returns: " + intObj.getData());

// initialize generic class
// with String data
GenericsClass<String> stringObj = new GenericsClass<>("Java Programming");
System.out.println("Generic Class returns: " + stringObj.getData());
}
}

// create a generics class
class GenericsClass<T> {

// variable of T type
private T data;

public GenericsClass(T data) {
this.data = data;
}

// method that return T type variable
public T getData() {
return this.data;
}
}