Skip to main content

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);
}
}

Comments

Popular posts from this blog

Using HyperSQL (HSQLDB)

HSQLDB is a portable RDBMS implemented in pure java. It can be embedded with your application as well as can be used separately. It is very a small database that supports almost all features of the standard database system. It comes with small jar file that can be found in lib folder. The HSQLDB jar package is located in the /lib directory of the ZIP package and contains several components and programs. Core components of jar file are : HyperSQL RDBMS Engine (HSQLDB), HyperSQL JDBC Driver, Database Manager, and Sql Tool. Installing and Using Download: download latest release of HyperSQL database from http://hsqldb.org website and extract it. You will see following contents. Here "bin" directory contains some batch files those can be used to run a swing based GUI tool. You can use runManagerSwing.bat to connect to database, but database must be on before running it. Directory lib contains File hsqldb.jar . It is the database to be used by you. Running database First

How to handle values from dynamically generated elements in web page using struts2

Some time you will see the form containing the button " Add More " . This facility is provided for the user to get the values for unknown number of repeating for some information. for example when you are asking to get the projects details from user, you need to put the option to add the more project for the user since you don't known how many projects user have. In the HTML form, you repeat the particular section to get the multiple values for those elements. In Html page , you can put the option to add new row of elements or text fields by writing the java script or using JQuery API. Now, the question is that how to capture the values of dynamically generated text fields on the server. Using the servlet programming you can get the values by using getParameters() method that resultants the array of the parameter having the same name. But this limit you to naming the text fields in the HTML form. To ally this approach, you have to take the same name for t

In Process Mode of HSQLDB in web application.

If you want to use the database into your web application, you can use the HSQLDB in In_Process mode. In this mode, you can embed the HSQLDB into your web application and it runs as a part of your web application programm in the same JVM. In this mode, the database does not open any port to connect to the application on the hosing machine and you don't need to configure anything to access it. Database is not expposed to other application and can not be accessed from any dabase tools like dbVisualizer etc. In this mode ,database will be unknown from any other person except you. But in the 1.8.0 version, you can use Server intance for external as well as in process access.  To close the databse, you can issue SHUTDOWN command as an SQL query.   In the in-process mode, database starts from JDBC with the associated databse file provided through  connection URL. for example   DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");   Here myd