Thursday, December 11, 2014

Java Best program



Write a simple generics class example 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 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();
        boundex bea = new boundex(new a());
        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");
    }
}

Generics wildcard arguments

package com.java2novice.generics;
public class mywildcardex
{
     public static void main(string a[])
       {      
        myemployeeutil empa
       = new myemployeeutil(new CompAEmp("Ram", 20000));
        myemployeeutil empb= new myemployeeutil
                    (new compbemp("krish", 30000));
        myemployeeutil empc= new myemployeeutil
                    (new compaemp("nagesh", 20000));
        system.out.println("Is salary same? "+empa.issalaryequal(empb));
        system.out.println("Is salary same? "+empa.issalaryequal(empc));
    }
}
class myemployeeutil
{
     
    private t emp;
    public myemployeeutil(t obj)
    {
        emp = obj;
    }
    public int getsalary()
   {
        return emp.getsalary();
    } 
    public boolean issalaryequal(myemployeeutil otheremp)
    {
           if(emp.getsalary() == otheremp.getsalary())
         {
            return true;
        }
        return false;
    }   
}
class emp
{
    private string name;
    private int salary;
    public emp(string name, int sal)
    {
        this.name = name;
        this.salary = sal;
    }
    public string getname()
   {
        return name;
    }
    public void setname(string name)
   {
        this.name = name;
    }
    public int getsalary()
   {
        return salary;
    }
    public void setsalary(int salary)
   {
        this.salary = salary;
    }
}
class compaemp extends emp
 {
    public compaemp(string nm, int sal)
   {
        super(nm, sal);
    }
}
class compbemp extends emp
{
    public compbemp(string nm, int sal)
   {
        super(nm, sal);
    }
}

No comments:

Post a Comment