Saturday, January 10, 2015

Java Using super class with generics and Java List Iterator



Write a simple generics class

package com.java2novice.generics;
public class MySimpleGenerics
{
public static void main(String a[])
{
SimpleGeneric sgs = new SimpleGeneric("JAVA2NOVICE");
sgs.printType();
SimpleGeneric sgb = new SimpleGeneric(Boolean.True);
sgb.printType();
    }
}
class SimpleGeneric
{
private T objReff = null;
public SimpleGeneric(T param)
   {
        this.objReff = param;
    }
public T getObjReff()
   {
        return this.objReff;
    }
public void printType()
   {
        System.out.println("Type: "+objReff.getClass().getName());
    }
}

Write a simple generics class with two type parameters

package com.java2novice.generics;
public class MySimpleTwoGenerics
{
 public static void main(String a[])
{
 SimpleGen sample= new SimpleGen("JAVA2NOVICE", 100);
        sample.printTypes();
    }
}
class SimpleGen
{
 private U objUreff;
 private V objVreff;
 public SimpleGen(U objU, V objV)
{
        this.objUreff = objU;
        this.objVreff = objV;
 }
  public void printTypes()
 {
        System.out.println("U Type: "+this.objUreff.getClass().getName());
        System.out.println("V Type: "+this.objVreff.getClass().getName());
    }
}

How implement bounded types extend super class with generics

package com.java2novice.generics;
public class MyBoundedClassEx
{
 public static void main(String a[])
{
 BoundEx bec = new BoundEx(new C());
 bec.doRunTest();
 BoundEx beb = new BoundEx(new B());
 beb.doRunTest();
 bea.doRunTest();
 BoundEx bes = new BoundEx(new String());
 bea.doRunTest();
 }
}
class BoundEx
{
private T objRef;
public BoundEx(T obj)
{
        this.objRef = obj;
  }
 public void doRunTest()
{
        this.objRef.printClass();
    }
}
class A
{
    public void printClass()
{
        System.out.println("I am in super class A");
    }
}

class B extends A
{
    public void printClass()
{
        System.out.println("I am in sub class B");
    }
}
class C extends A
{
    public void printClass()
{
        System.out.println("I am in sub class C");
    }
}

How implement bounded types with generics

package com.java2novice.generics;
public class MyBoundedInterface
{
public static void main(String a[])
{
          BoundExmp bey = new BoundExmp(new Y());
bey.doRunTest();
BoundExmp bez = new BoundExmp(new Z());
bez.doRunTest();
BoundExmp bes = new BoundExmp(new String());
bea.doRunTest();
    }
}
class BoundExmp X>
{
 private T objRef;
 public BoundExmp(T obj)
{
        this.objRef = obj;
 }
 public void doRunTest()
{
        this.objRef.printClass();
    }
}
interface X
{
    public void printClass();
}

class Y implements X
{
public void printClass()
{
        System.out.println("I am in class Y");
    }
}
class Z implements X
{
public void printClass()
{
        System.out.println("I am in class Z");
    }
}

Java List Iterator

package com.myjava.listiterator;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class MyListIterator
{
 public static void main(String a[])
{
        List li = new ArrayList();
        ListIterator litr = null;
        li.add(23);
        li.add(98);
        li.add(29);
        li.add(71);
        li.add(5);
        litr=li.listIterator();
        System.out.println("Elements in forward directiton");
        while(litr.hasNext())
        {
            System.out.println(litr.next());
 }
        System.out.println("Elements in backward directiton");
        while(litr.hasPrevious())
         {
            System.out.println(litr.previous());
        }
    }
}

No comments:

Post a Comment