Java Generic Examples Source Code

/Simple generic class
class A<T>{
T obj;
T get(){
return obj;
}
void set(T o){
obj=o;
}
}

class Gen1{
public static void main(String ar[]){
//Creating object of Generic class
A<Integer> a1;//reference variable declaration
a1=new A<Integer>();//object creation
a1.set(123);//Autoboxing is used to convert int type to Integer type
int i=a1.get();//Auto un-boxing is used to convert Integer type to int type
System.out.println(i);
}
}


//Simple generic class
//with two type-parameters
class A<T,K>{
T obj1;
K obj2;

T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}

K getObj2(){
return obj2;
}
void setObj2(K o){
obj2=o;
}
}

class Gen1{
public static void main(String ar[]){

A<Integer,String> a1;
a1=new A<Integer,String>();
a1.setObj1(123);
int i=a1.getObj1();
System.out.println(i);

a1.setObj2("This is Test");
String s=a1.getObj2();
System.out.println(s);
}
}


//Simple generic class
//Using type-parameter of user defined class
class X{int x=10;}
class Y{int y=20;}
class Z{int z=30;}

class A<T>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}
}

class Gen3{
public static void main(String ar[]){

A<X> a1=new A<X>();
a1.setObj1(new X());
X objX=a1.getObj1();
System.out.println(objX.x);
}
}


//Simple generic class
//If type-parameter are super type then object of sub classes can be hold
class X{int x=10;}
class Y extends X{int y=20;}
class Z extends X{int z=30;}

class A<T>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}
}

class Gen4{
public static void main(String ar[]){
A<X> a1=new A<X>();
a1.setObj1(new Y());
Y o1=(Y)a1.getObj1();
System.out.println(o1.y);

A<Y> a2=new A<Y>();
a2.setObj1(new Y());
Y o2=(Y)a2.getObj1();
System.out.println(o2.y);

/* It will give Error
A<Y> a3=new A<Y>();
a3.setObj1(new X());
X o3=(X)a3.getObj1();
System.out.println(o3.x);*/
}
}


//Simple generic class
//Bounded Type
class X{int x=10;}
class Y extends X{int y=20;}
class Z extends X{int z=30;}
class P {int p=40;}
//Here T can be relaced by X of subclass of X
class A<T extends X>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(T o){
obj1=o;
}
}

class Gen5{
public static void main(String ar[]){
A<X> a1=new A<X>();
a1.setObj1(new X());
X o1=a1.getObj1();
System.out.println(o1.x);

A<Y> a2=new A<Y>();
a2.setObj1(new Y());
Y o2=a2.getObj1();
System.out.println(o2.y);
//Compile time Error
/*A<P> a3=new A<P>();
a3.setObj1(new P());
P o3=a3.getObj1();
System.out.println(o3.p);*/
}
}


//Simple generic class
//Bounded Type
class X{int x=10;}
class Y extends X{int y=20;}
class Z extends X{int z=30;}
class P {int p=40;}
//Here T can be relaced by X of subclass of X
class A<T extends X>{
T obj1;
T getObj1(){
return obj1;
}
void setObj1(A<?> o){
obj1=o;
}
}

class Gen5{
public static void main(String ar[]){
A<X> a1=new A<X>();
a1.setObj1(new P());
X o1=a1.getObj1();
System.out.println(o1.x);
}
}


//Simple generic class
//Wildcard
class X{int val=10;}
class Y extends X{int val=20;}
class Z extends X{int val=30;}
class P {int val=40;}
//Here T can be relaced by X of subclass of X
class A<T>{
T obj;
A(T o){
obj=o;
}
}
class B{
//this method will take argument of type A,
//but it no need to care about that, what type of type-parameter A class will have
void setObj1(A o){
System.out.println(o.obj.getClass().getName());
}
}
class Gen7{
public static void main(String ar[]){
B b=new B();
A<X> a1=new A<X>(new Y());
b.setObj1(a1);
}
}


//Simple generic class
//Bounded-Wildcard
class X{int val=10;}
class Y extends X{int val=20;}
class Z extends X{int val=30;}
class P {int val=40;}
//Here T can be relaced by X of subclass of X
class A<T>{
T obj;
A(T o){
obj=o;
}
}
class B{
//this method will take argument of type A,
//but it no need to care about that, what type of type-parameter A class will have
//here method will accept type-args of type of X or sub-class X
void setObj1(A<? extends X> o){
System.out.println(o.obj.getClass().getName());
}
}
class Gen8{
public static void main(String ar[]){
B b=new B();
A<Y> a1=new A<Y>(new Y());
b.setObj1(a1);
}
}


//Simple generic Method
class A{
<T>void print(T a)
{
System.out.println("Recieved value : "+a);
}
}
class Gen9{
public static void main(String ar[]){
A a=new A();
a.print(100);
}
}


//Simple generic Method with more type-parameter
class A{
<T,E>void print(T a,E b)
{
System.out.println("Recieved value : "+a);
System.out.println("Recieved value : "+b);
}
}
class Gen10{
public static void main(String ar[]){
A a=new A();
a.print(100,"Strig");
}
}


//Simple generic Method with relation b/w more type-parameter
class A{
<T,E extends T>void print(T a,E b)
{
System.out.println("Recieved value : "+a);
System.out.println("Recieved value : "+b);
}
}
class Gen11{
public static void main(String ar[]){
A a=new A();
//a.print(100,"Strig");
//second arg must of type of first arg or subtype of first arg.
a.print(100,200);
}
}


//Simple generic Method with relation b/w more type-parameter
class x{int val=10;}
class y extends x{int val=10;}
class A{
<T,E extends T>void print(T a,E b)
{
System.out.println("Recieved value : "+a);
System.out.println("Recieved value : "+b);
}
}
class Gen11{
public static void main(String ar[]){
A a=new A();
a.print(new x(),new y());
a.print(new x(),new x());
a.print(new y(),new y());
//a.print(new y(),new x());//Compile time error
}
}


//Simple generic Method with relation b/w more type-parameter
//generic return type
class x{int val=10;}
class y extends x{int val=10;}
class A{
<T,E extends T> T print(E a)
{
T var=a;
return var;
}
}
class Gen13{
public static void main(String ar[]){
A a=new A();
int p=a.print(10);
System.out.println(p);
}
}


//Simple generic constructor
class A{
<E> A(E a)
{
System.out.println(a);
}
}
class Gen14{
public static void main(String ar[]){
A a=new A(1);
}
}


//Generic interface
interface Intf<T>{
void print(T a);
}
class A<T> implements Intf<T>{
public void print(T a){
System.out.println(a);
}
}
class Gen15{
public static void main(String ar[]){
A a=new A();
a.print(10.98);
}
}


//Generic interface
interface Intf<T>{
void print(T a);
}
class A<E extends T> implements Intf<T>{
public void print(T a){
System.out.println(a);
}
}
class Gen15{
public static void main(String ar[]){
A a=new A();
a.print(10.98);
}
}


//Generic subclass and supercalss
class A<T>{
T o;
}
//E is own type-parameter of B and T is for Super class
class B<T,E> extends A<T>{
public void print(T a){
o=a;
System.out.println(a);
}
}
class Gen17{
public static void main(String ar[]){
B a=new B();
a.print(10.98);
}
}

No comments:

Popular Posts