Java Generics


Generics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods.

Source Code

class Box {
    private T t;
    public void set(T t) { this.t = t; }
    public T get() { return t; }
}
public class Test {
    public static void main(String[] args) {
        Box integerBox = new Box();
        Box stringBox = new Box();

        integerBox.set(10);
        stringBox.set("Hello World");

        System.out.println(integerBox.get());
        System.out.println(stringBox.get());
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments