GENERIC CLASSES:

Sample Input and Output:

Enter an integer :
15
Enter a string :
Hello World Generic class
Integer Value :15
String Value :Hello World Generic class

CODE:

// Generic Class


public class Item <T> {

    

T data;


public T getData() {

return data;

}


public void setData(T data) {

this.data = data;

}

}


import java.io.IOException;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {

    Item <Integer> item1 = new Item<Integer>();

Item <String> item2 = new Item<String>();

Scanner sc = new Scanner(System.in);

System.out.println("Enter a integer :");

int num = Integer.parseInt(sc.nextLine());

System.out.println("Enter a string :");

String str = sc.nextLine();

item1.setData(num);

item2.setData(str);

System.out.println("Integer Value :"+item1.getData());

System.out.println("String Value :"+item2.getData());

}

}

Comments

Popular posts from this blog