Generic Classes

 ITEM.JAVA:

public class Item <T> {
private T t;
public void set(T t){
this.t=t;
}
public T get(){
return t;
}
}




MAIN.JAVA:

import java.util.Scanner;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
Item<Integer> integerItem=new Item<Integer>();
Item<String> stringItem=new Item<String>();
System.out.println("Enter a integer:");
integerItem.set(sc.nextInt());
sc.nextLine();
System.out.println("Enter a string :");
stringItem.set(sc.nextLine());
System.out.println("Integer Value :"+integerItem.get());
System.out.println("String Value: "+stringItem.get());
}
}

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.