Generics

Monday(09-01-2017)

package com.lara;

class A

{

            String i;

            Integer j;

            Double k;

}

public class Manager1

{

            public static void main(String[] args)

            {

                        A a1 = new A();

                        a1.i = "abc";

                        a1.j = 20;

                        a1.k = 45.89;

                       

                        A a2 = new A();

                        a2.i = "abc";

                        a2.j = 20;

                        a2.k = 45.89;

                       

                        System.out.println("done");

                       

            }

}

package com.lara;

class B

{

            void test1(String s1)

            {

                        System.out.println("test1:" + s1);

            }

            Integer test2()

            {

                        System.out.println("test2");

                        return 30;

            }

}

public class Manager2

{

            public static void main(String[] args)

            {

                        B b1 = new B();

                        b1.test1("abc");

                        int i = b1.test2();

                       

                       

                        B b2 = new B();

                        b2.test1("xyz");

                        int j = b2.test2();

                       

                        System.out.println("done");

            }

}

package com.lara;

class C

{

            C(String s1)

            {

                        System.out.println("C(String)");

            }

}

public class Manager3

{

            public static void main(String[] args)

            {

                        C c1 = new C("abc");

                        C c2 = new C("xyz");

                        System.out.println("done");

            }

}

package com.lara;

class D < X >

{

            String s1;

            X s2;

}

public class Manager4

{

            public static void main(String[] args)

            {

                        D d1 = new D();

                        d1.s1 = "abc";

                        d1.s2 = new Object();

                        d1.s2 = "xyz";

                        d1.s2 = 456;

                        d1.s2 = 89.787;

                        d1.s2 = true;

                        d1.s2 = 'a';

                        d1.s2 = new D();

                       

                        D<Integer> d2 = new D<Integer>();

                        d2.s1 = "hello";

                        d2.s2 = 300;

                        //d2.s2 = "abc";

                       

                       

                        D<String> d3 = new D<String>();

                        d3.s1 = "hello";

                        //d3.s2 = 300;

                        d3.s2 = "abc";

                       

                        D<Double> d4 = new D<Double>();

                        d4.s1 = "hello";

                        d4.s2 = 300.89;

                        //d4.s2 = "abc";

                        //d4.s2 = 78;

                       

            }

}

package com.lara;

class E <GenDataType>

{

            Integer field1;

            GenDataType field2;

            void test1(String str)

            {

                        System.out.println("from test1(String)");

            }

            void test2(GenDataType arg)

            {

                        System.out.println("from test2(GenDataType)");

            }

            Double test3()

            {

                        return 34.89;

            }

            GenDataType test4()

            {

                        return field2;

            }

}

public class Manager5

{

            public static void main(String[] args)

            {

                        E<String> e1 = new E<>();

                        e1.field1 = 24;

                        e1.field2 = "abc";

                        e1.test1("abc");

                        e1.test2("hello");

                        double i = e1.test3();

                        String j = e1.test4();

 

                       

                        E<Integer> e2 = new E<>();

                        e2.field1 = 24;

                        e2.field2 = 9090;

                        e2.test1("abc");

                        e2.test2(7878);

                        double m = e2.test3();

                        Integer n = e2.test4();

 

                        E<Double> e3 = new E<>();

                        e3.field1 = 24;

                        e3.field2 = 90.90;

                        e3.test1("abc");

                        e3.test2(78.78);

                        double p = e3.test3();

                        double q = e3.test4();

 

                        E<E> e4 = new E<>();

                        e4.field1 = 24;

                        e4.field2 = new E();

                        e4.test1("abc");

                        e4.test2(new E());

                        double r = e4.test3();

                        E s = e4.test4();

 

                       

            }

}

package com.lara;

class F < E >

{

            E field1;

            F(E arg1)

            {

                        field1 = arg1;

            }

            void setField1(E arg1)

            {

                        field1 = arg1;

            }

            E getField1()

            {

                        return field1;

            }

}

public class Manager6

{

            public static void main(String[] args)

            {

                        F<Integer> f1= new F<>(10);

                        int i = f1.getField1();

                        System.out.println(i);

                        f1.setField1(20);

                        i = f1.getField1();

                        System.out.println(i);

                       

                        F<String> f2= new F<>("abc");

                        String j = f2.getField1();

                        System.out.println(j);

                        f2.setField1("xyz");

                        j = f2.getField1();

                        System.out.println(j);

                       

                        F<Double> f3= new F<>(50.89);

                        Double k = f3.getField1();

                        System.out.println(k);

                        f3.setField1(7.676);

                        k = f3.getField1();

                        System.out.println(k);

                       

                        F<F> f4 = new F<>(null);

                        F obj = f4.getField1();

                        System.out.println(obj);

                        f4.setField1(new F(new Object()));

                        obj = f4.getField1();

                        System.out.println(obj);

            }

}

package com.lara;

class G<X, Y>

{

            X field1;

            Y field2;

            X field3;        

            G(X arg1, Y arg2)

            {

                        field1 = arg1;

                        field2 = arg2;

                        field3 = arg1;

            }

            public X getField1()

            {

                        return field1;

            }

            public void setField1(X field1)

            {

                        this.field1 = field1;

            }

            public Y getField2()

            {

                        return field2;

            }

            public void setField2(Y field2)

            {

                        this.field2 = field2;

            }

            public X getField3()

            {

                        return field3;

            }

            public void setField3(X field3)

            {

                        this.field3 = field3;

            }

}

public class Manager7

{

            public static void main(String[] args)

            {

                        G<Integer, String> g1 = new G<>(10, "abc");

                        int i = g1.getField1();

                        System.out.println(i);

                        String j = g1.getField2();

                        System.out.println(j);

                        i = g1.getField3();

                        System.out.println(i);

                        g1.setField1(20);

                        g1.setField2("xyz");

                        g1.setField3(30);               

                        i = g1.getField1();

                        System.out.println(i);

                        j = g1.getField2();

                        System.out.println(j);

                        i = g1.getField3();

                        System.out.println(i);

           

            }

}