AkhmadGhafirin
5/16/2017 - 2:48 AM

Generic

package generic;

/**
 * Created by ahmad on 05/05/17.
 */
public class GenClass <T>{
    T ob; // declare an object of type T
    GenClass(T o) {
        ob = o;
    }
    T getob() {
        return ob;
    }
    void showType() {
        System.out.println("Type of T is " + ob.getClass().getName());
    }
}


===================================================================================================================


package generic;

/**
 * Created by ahmad on 05/05/17.
 */
public class GenClassDemo {
    public static void main(String[] args) {
        GenClass<Integer> iOb;
        iOb = new GenClass<>(88);
        iOb.showType();
        int v = iOb.getob();
        System.out.println("value: " + v);
        System.out.println();
        GenClass<String> strOb = new GenClass<>("Generics Test");
        String v2 = strOb.getob();

        GenClass<String> xOb;
        xOb = new GenClass<>("Generic");
        xOb.showType();
        String str = xOb.getob();
        System.out.println(str);
        System.out.println();
        GenClass<String> strXOb = new GenClass<>("Generic Test");
        String str2 = strXOb.getob();

    }

    public static<T> void print(T t){
        System.out.println("Aku mencetak : " + t.toString());
    }
}