import java.util.ArrayList;
/*
* example of using Generics
*/
public class Library<T> {
private ArrayList<T> mediaCollection = new ArrayList<T>();
public Library() {
Book b = new Book();
storeMedia(b);
Video v = new Video();
storeMedia(v);
Newspaper n = new Newspaper();
storeMedia(n);
showMedia();
}
public void showMedia() {
System.out.println("These items are in the media collection:");
for (Object resource : mediaCollection.toArray()) {
System.out.print("\t" + resource.toString() + "\n");
}
}
public void storeMedia(T item) {
mediaCollection.add(item);
}
public class Book implements Media {
String type = "book";
String name = "Robinson Curuso";
public Book(){
System.out.println(this.getClass());
}
public String toString() {
return "type is " + type + ", name is: " + name;
}
}
class Video implements Media {
String type = "video";
String name = "Elvis Pressley in the Army";
public String toString() {
return "type is " + type + ", name is: " + name;
}
}
class Newspaper {
String type = "newspaper";
String name = "News & Disturber";
public String toString() {
return "type is " + type + ", name is: " + name;
}
}
interface Media {
// String type;
}
public static void main(String[] args) {
Library<Media> lib = new Library<Media>();
}
}
--- Update ---
Current error is on line 13 the "storeMedia(b);" and other two similar lines. Error message, "method storeMedia in class Library<T> cannot be applied to given types, required T, found Library<T>.Book.
I have made many attempts trying to get clean compile. I think I am missing a basic concept so an explanation along with the code remedy would be appreciated. Thanks in advance