0 votes
1.1k views
in Java by
Any one can give an example for generic.

2 Answers

0 votes
by
package com; public class MyGeneric { private T t; public T getT() { return t; } public void setT(T t) { this.t = t; } /** * @param args */ public static void main(String[] args) { MyGeneric strGen = new MyGeneric(); strGen.setT("Hello World"); MyGeneric intGen = new MyGeneric(); intGen.setT(50); System.out.println("Getting String: " + strGen.getT()); System.out.println("Getting Integer: " + intGen.getT()); } }
0 votes
by
package com;
 
public class MyGeneric<T> {
 
private T t;
 
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
/**
* @param args
*/
public static void main(String[] args) {
MyGeneric<String> strGen = new MyGeneric<String>();
strGen.setT("Hello World");
MyGeneric<Integer> intGen = new MyGeneric<Integer>();
intGen.setT(50);
System.out.println("Getting String: " + strGen.getT());
System.out.println("Getting Integer: " + intGen.getT());
}
 
}
 

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...