Lara

Generics

Wednesday(8-2-2017)

package com.lara;

 

class A

{

            String s1;

}

 

public class M1

{

            public static void main(String[] args)

            {

                        A a1 = new A();

                        a1.s1 = "abc";

                        String str = a1.s1;

                       

                        A a2 = new A();

                        a2.s1 = "xyz";

                        String str1 = a2.s1;

                       

                        System.out.println("done");

                       

            }

}

package com.lara;

class B

{

            Object obj;

}

public class M2

{

            public static void main(String[] args)

            {

                        B b1 = new B();

                        b1.obj = "abc";

                       

                        B b2 = new B();

                        b2.obj = 20;

                       

                        Integer s1 = (Integer) b1.obj;

                       

                        int i = (Integer) b2.obj;

                       

                        System.out.println(s1);

                        System.out.println(i);

            }

}

package com.lara;

class C < X >

{

            X obj;

}

public class M3

{

            public static void main(String[] args)

            {

                        C c1 = new C();

                        C<String> c2 = new C<String>();

                        System.out.println(c1);

                        System.out.println(c2);

                        c2.obj = "abc";

                        String s1 = c2.obj;

                       

                        C<Integer> c3 = new C<Integer>();

                        c3.obj = 1020;

                        int i = c3.obj;

                       

                        C<Double> c4 = new C<Double>();

                        c4.obj = 12.89;

                        double j = c4.obj;

                       

                        C<Boolean> c5 = new C<>();

                        c5.obj = true;

                        boolean b1 = c5.obj;

                       

                        //C<int> c6 = new C<>();//

                                               

                        System.out.println("done");

                       

            }

}

package com.lara;

class D <X extends Number>

{

            X obj;

}

public class M4

{

            public static void main(String[] args)

            {

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

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

//                     D<Boolean> d3 = new D<>();

//                     D<String> d4 = new D<>();

                        D d5 = new D();

                       

            }

}

package com.lara;

 

class E < X >

{

            X obj;

}

public class M5

{

            static void test(E<?> e1)

            {

                        //e1.obj = 12;

            }

            public static void main(String[] args)

            {

            }

}

package com.lara;

 

import java.util.ArrayList;

 

public class M6

{

            static void test(ArrayList<?> list)

            {

                        System.out.println(list);

                        System.out.println(list.get(3));

                        //list.add(90);

            }

}

package com.lara;

class F < X >

{

            X obj;

}

public class M7

{

            public static void main(String[] args)

            {

                        F<? extends Number> f1 = null;

                        f1 = new F<Integer>();

                        f1 = new F<Double>();

                        f1 = new F<Byte>();

//                     f1 = new F<Object>();

//                     f1 = new F<String>();

                        f1 = new F<Number>();

                        //f1.obj = new Byte(90);

            }

}

package com.lara;

 

import java.util.ArrayList;

 

public class M8

{

            static void test(ArrayList<? extends Number> list)

            {

                        //list.add(new Byte(90));

            }

}

package com.lara;

 

class G < X >

{

            X obj;

}

 

public class M9

{

            public static void main(String[] args)

            {

                        G <? super Number> g1 = null;

                        g1 = new G<Number>();

                        g1 = new G<Object>();

//                     g1 = new G<Byte>();

//                     g1 = new G<Integer>();

//                     g1 = new G<String>();

                       

                        g1.obj = 100;

                        g1.obj = 5656.756757;

                       

            }

}